Added an option for user credentials on user menu
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileUploadDialog.java
index aef6b5a..3bf47cd 100644 (file)
  */
 package gr.grnet.pithos.web.client;
 
-import com.google.gwt.core.client.Scheduler;
-import com.google.gwt.http.client.Response;
 import gr.grnet.pithos.web.client.foldertree.File;
 import gr.grnet.pithos.web.client.foldertree.Folder;
-import gr.grnet.pithos.web.client.foldertree.Resource;
-import gr.grnet.pithos.web.client.rest.GetCommand;
-import gr.grnet.pithos.web.client.rest.PostCommand;
-import gr.grnet.pithos.web.client.rest.PutRequest;
-import gr.grnet.pithos.web.client.rest.RestCommand;
-import gr.grnet.pithos.web.client.rest.RestException;
-import gr.grnet.pithos.web.client.rest.resource.FileResource;
-import gr.grnet.pithos.web.client.rest.resource.FolderResource;
-import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
-import gr.grnet.pithos.web.client.rest.resource.UploadStatusResource;
-
-import java.util.ArrayList;
-import java.util.List;
 
 import com.google.gwt.core.client.GWT;
 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.http.client.URL;
-import com.google.gwt.json.client.JSONObject;
-import com.google.gwt.json.client.JSONString;
-import com.google.gwt.user.client.DeferredCommand;
+import com.google.gwt.user.client.Command;
+import com.google.gwt.user.client.Cookies;
 import com.google.gwt.user.client.Event.NativePreviewEvent;
-import com.google.gwt.user.client.Timer;
+import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.DialogBox;
 import com.google.gwt.user.client.ui.FileUpload;
@@ -72,13 +55,11 @@ import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
 import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
 import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
 import com.google.gwt.user.client.ui.Grid;
-import com.google.gwt.user.client.ui.HTML;
 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 import com.google.gwt.user.client.ui.Hidden;
 import com.google.gwt.user.client.ui.HorizontalPanel;
 import com.google.gwt.user.client.ui.Label;
 import com.google.gwt.user.client.ui.VerticalPanel;
-import javax.xml.transform.Templates;
 
 /**
  * The 'File upload' dialog box implementation.
@@ -90,7 +71,7 @@ public class FileUploadDialog extends DialogBox {
        /**
         * The Form element that performs the file upload.
         */
-       private final FormPanel form = new FormPanel();
+    protected final FormPanel form = new FormPanel();
 
        private final FileUpload upload = new FileUpload();
 
@@ -102,7 +83,7 @@ public class FileUploadDialog extends DialogBox {
 
        protected Folder folder;
 
-    protected GSS app;
+    protected Pithos app;
 
        /**
         * The widget's constructor.
@@ -119,15 +100,10 @@ public class FileUploadDialog extends DialogBox {
                // Create a panel to hold all of the form widgets.
                VerticalPanel panel = new VerticalPanel();
                form.setWidget(panel);
-               final HTML info = new HTML("You may select a file to upload. Install" +
-                               " <a href='http://gears.google.com/' target='_blank'>Google " +
-                               "Gears</a><br> for uploading multiple files simultaneously.");
-               info.addStyleName("pithos-uploadNote");
-               panel.add(info);
 
         final Hidden auth = new Hidden("X-Auth-Token", "");
         panel.add(auth);
-               upload.setName("file");
+               upload.setName("X-Object-Data");
                filenameLabel.setText("");
                filenameLabel.setVisible(false);
                filenameLabel.setStyleName("props-labels");
@@ -154,7 +130,7 @@ public class FileUploadDialog extends DialogBox {
                // form.
                submit = new Button("Upload", new ClickHandler() {
                        @Override
-                       public void onClick(ClickEvent event) {
+                       public void onClick(@SuppressWarnings("unused") ClickEvent event) {
                                prepareAndSubmit();
                        }
                });
@@ -164,14 +140,13 @@ public class FileUploadDialog extends DialogBox {
                // dialog when the button is clicked.
                final Button cancel = new Button("Cancel", new ClickHandler() {
                        @Override
-                       public void onClick(ClickEvent event) {
+                       public void onClick(@SuppressWarnings("unused") ClickEvent event) {
                                hide();
                        }
                });
                buttons.add(cancel);
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
                buttons.setSpacing(8);
-               buttons.addStyleName("pithos-DialogBox");
         panel.add(buttons);
         panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
 
@@ -179,8 +154,9 @@ public class FileUploadDialog extends DialogBox {
                form.addSubmitHandler(new SubmitHandler() {
 
                        @Override
-                       public void onSubmit(SubmitEvent event) {
+                       public void onSubmit(@SuppressWarnings("unused") SubmitEvent event) {
                 auth.setValue(app.getToken()); //This is done here because the app object is not available in the constructor
+                Cookies.setCookie("X-Auth-Token", app.getToken(), null, Window.Location.getHostName(), app.getApiPath(), false);
                        }
                });
                form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
@@ -195,18 +171,24 @@ public class FileUploadDialog extends DialogBox {
 
                                // Unfortunately the results are never empty, even in
                                // the absense of errors, so we have to check for '<pre></pre>'.
-                               if (results != null && !results.equalsIgnoreCase("<pre></pre>")) {
+                               if (results != null && results.length() > 0 && !results.equalsIgnoreCase("<pre></pre>")) {
                                        GWT.log(results, null);
                                        app.displayError(results);
                                }
-                app.updateFolder(folder);
+                               if (app.getSelectedTree().equals(app.getFolderTreeView()))
+                                       app.updateFolder(folder, true, new Command() {
+                                               
+                                               @Override
+                                               public void execute() {
+                                                       app.updateStatistics();
+                                               }
+                                       });
+                               else
+                                       app.updateOtherSharedFolder(folder, true);
                                hide();
                        }
                });
 
-
-               panel.addStyleName("pithos-DialogBox");
-               addStyleName("pithos-DialogBox");
                setWidget(form);
        }
 
@@ -237,7 +219,7 @@ public class FileUploadDialog extends DialogBox {
             return;
         }
         final String fname = getFilename(upload.getFilename());
-        String apath = app.getApiPath() + app.getUsername() + folder.getUri() + "/" + fname;
+        String apath = app.getApiPath() + folder.getOwner() + folder.getUri() + "/" + fname;
         form.setAction(apath);
         submit.setEnabled(false);
         upload.setVisible(false);
@@ -245,7 +227,7 @@ public class FileUploadDialog extends DialogBox {
         filenameLabel.setVisible(true);
 
                if (getFileForName(fname) == null) {
-                       doUpload(apath);
+            form.submit();
                }
                else {
                        // We are going to update an existing file, so show a confirmation dialog.
@@ -267,28 +249,6 @@ public class FileUploadDialog extends DialogBox {
                }
        }
 
-    private void doUpload(String path) {
-        PutRequest createFile = new PutRequest(path) {
-            @Override
-            public void onSuccess(Resource result) {
-                form.submit();
-            }
-
-            @Override
-            public void onError(Throwable t) {
-                GWT.log("", t);
-                if (t instanceof RestException) {
-                    app.displayError("Unable to create file:" + ((RestException) t).getHttpStatusText());
-                }
-                else
-                    app.displayError("System error creating file:" + t.getMessage());
-            }
-        };
-        createFile.setHeader("X-Auth-Token", app.getToken());
-        createFile.setHeader("Content-Length", "0");
-        Scheduler.get().scheduleDeferred(createFile);
-    }
-
     /**
         * 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
@@ -310,12 +270,12 @@ public class FileUploadDialog extends DialogBox {
 
        protected File getFileForName(String name){
                for (File f : folder.getFiles())
-                       if (!f.isInTrash() && f.getName().equals(name))
+                       if (f.getName().equals(name))
                                return f;
                return null;
        }
 
-    public void setApp(GSS app) {
+    public void setApp(Pithos app) {
         this.app = app;
     }