Implemented background multiple upload (issue #1989)
[pithos-web-client] / src / gr / grnet / pithos / web / client / Pithos.java
index dc5ec4f..2adb7fd 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
@@ -54,11 +54,8 @@ import gr.grnet.pithos.web.client.rest.HeadRequest;
 import gr.grnet.pithos.web.client.rest.PutRequest;
 import gr.grnet.pithos.web.client.rest.RestException;
 import gr.grnet.pithos.web.client.tagtree.Tag;
-import gr.grnet.pithos.web.client.tagtree.TagTreeView;
-import gr.grnet.pithos.web.client.tagtree.TagTreeViewModel;
 
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -99,7 +96,6 @@ import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 import com.google.gwt.user.client.ui.HasVerticalAlignment;
 import com.google.gwt.user.client.ui.HorizontalPanel;
 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
-import com.google.gwt.user.client.ui.Image;
 import com.google.gwt.user.client.ui.RootPanel;
 import com.google.gwt.user.client.ui.VerticalPanel;
 import com.google.gwt.view.client.SelectionChangeEvent;
@@ -114,6 +110,8 @@ public class Pithos implements EntryPoint, ResizeHandler {
        public static final String HOME_CONTAINER = "pithos";
 
        public static final String TRASH_CONTAINER = "trash";
+
+       public static final Configuration config = GWT.create(Configuration.class);
        
        /**
         * Instantiate an application-level image bundle. This object will provide
@@ -141,6 +139,10 @@ public class Pithos implements EntryPoint, ResizeHandler {
         groupTreeView.updateGroupNode(group);
     }
 
+    public void updateMySharedRoot() {
+       mysharedTreeView.updateRoot();
+    }
+    
     public void updateSharedFolder(Folder f, boolean showfiles) {
        mysharedTreeView.updateFolder(f, showfiles);
     }
@@ -177,6 +179,8 @@ public class Pithos implements EntryPoint, ResizeHandler {
                ImageResource tools();
        }
 
+       private Throwable error;
+       
        /**
         * The Application Clipboard implementation;
         */
@@ -190,7 +194,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
        /**
         * The panel that contains the various system messages.
         */
-       private MessagePanel messagePanel = new MessagePanel(Pithos.images);
+       private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
 
        /**
         * The bottom panel that contains the status bar.
@@ -245,7 +249,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
     GroupTreeViewModel groupTreeViewModel;
     private GroupTreeView groupTreeView;
 
-    private TreeView selectedTree;
+    TreeView selectedTree;
     protected AccountResource account;
     
     Folder trash;
@@ -264,8 +268,10 @@ public class Pithos implements EntryPoint, ResizeHandler {
     
     private HTML numOfFiles;
     
-    private Image toolsButton;
-
+    private Toolbar toolbar;
+    
+    private FileUploadDialog fileUploadDialog;
+    
        @Override
        public void onModuleLoad() {
                if (parseUserCredentials())
@@ -289,46 +295,33 @@ public class Pithos implements EntryPoint, ResizeHandler {
                outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
         }
         
+        messagePanel.setVisible(false);
+        outer.add(messagePanel);
+        outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
+        outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
+
         HorizontalPanel header = new HorizontalPanel();
         header.addStyleName("pithos-header");
         header.setWidth(contentWidth);
         if (bareContent)
                header.addStyleName("pithos-header-noframe");
-        upload = new Button("Upload File", new ClickHandler() {
+        upload = new Button("Upload", new ClickHandler() {
             @Override
-            public void onClick(@SuppressWarnings("unused") ClickEvent event) {
-                new UploadFileCommand(Pithos.this, null, getSelection()).execute();
+            public void onClick(ClickEvent event) {
+               if (getSelection() != null)
+                       new UploadFileCommand(Pithos.this, null, getSelection()).execute();
             }
         });
         upload.addStyleName("pithos-uploadButton");
         header.add(upload);
         header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
         header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
-//        header.setCellWidth(upload, "146px");
 
-        messagePanel.setVisible(false);
-        header.add(messagePanel);
-        header.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
-        header.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
+        toolbar = new Toolbar(this);
+        header.add(toolbar);
+        header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
+        header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
         
-        toolsButton = new Image(images.tools());
-        toolsButton.addStyleName("pithos-toolsButton");
-        toolsButton.addClickHandler(new ClickHandler() {
-                       
-                       @Override
-                       public void onClick(ClickEvent event) {
-                ToolsMenu menu = new ToolsMenu(Pithos.this, images, getSelectedTree(), getSelectedTree().getSelection(), getFileList().getSelectedFiles());
-                if (!menu.isEmpty()) {
-                           menu.setPopupPosition(event.getClientX(), event.getClientY());
-                           menu.show();
-                }
-                       }
-               });
-        header.add(toolsButton);
-        header.setCellHorizontalAlignment(toolsButton, HasHorizontalAlignment.ALIGN_CENTER);
-        header.setCellVerticalAlignment(toolsButton, HasVerticalAlignment.ALIGN_MIDDLE);
-        header.setCellWidth(toolsButton, "40px");
-       
         HorizontalPanel folderStatistics = new HorizontalPanel();
         folderStatistics.addStyleName("pithos-folderStatistics");
         numOfFiles = new HTML();
@@ -350,13 +343,20 @@ public class Pithos implements EntryPoint, ResizeHandler {
         folderTreeSelectionModel = new SingleSelectionModel<Folder>();
         folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
             @Override
-            public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
+            public void onSelectionChange(SelectionChangeEvent event) {
                 if (folderTreeSelectionModel.getSelectedObject() != null) {
                     deselectOthers(folderTreeView, folderTreeSelectionModel);
                     applyPermissions(folderTreeSelectionModel.getSelectedObject());
                     Folder f = folderTreeSelectionModel.getSelectedObject();
-                    showFiles(f);
+                       updateFolder(f, true, new Command() {
+                               
+                               @Override
+                               public void execute() {
+                                       updateStatistics();
+                               }
+                       });
                 }
+                showRelevantToolbarButtons();
             }
         });
         selectionModels.add(folderTreeSelectionModel);
@@ -510,14 +510,12 @@ public class Pithos implements EntryPoint, ResizeHandler {
                 s.setSelected(s.getSelectedObject(), false);
     }
 
-    public void showFiles(Folder f) {
+    public void showFiles(final Folder f) {
         Set<File> files = f.getFiles();
         showFiles(files);
     }
 
     public void showFiles(Set<File> files) {
-        //Iterator<File> iter = files.iterator();
-        //fetchFile(iter, files);
         fileList.setFiles(new ArrayList<File>(files));
     }
 
@@ -527,13 +525,14 @@ public class Pithos implements EntryPoint, ResizeHandler {
             String path = file.getUri() + "?format=json";
             GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
                 @Override
-                public void onSuccess(@SuppressWarnings("unused") File _result) {
+                public void onSuccess(File _result) {
                     fetchFile(iter, files);
                 }
 
                 @Override
                 public void onError(Throwable t) {
                     GWT.log("Error getting file", t);
+                                       setError(t);
                     if (t instanceof RestException)
                         displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
                     else
@@ -588,8 +587,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
         * Redirect the user to the login page for authentication.
         */
        protected void authenticateUser() {
-               Configuration conf = (Configuration) GWT.create(Configuration.class);
-        Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
+        Window.Location.assign(config.loginUrl());
        }
 
        protected void fetchAccount(final Command callback) {
@@ -606,6 +604,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
             @Override
             public void onError(Throwable t) {
                 GWT.log("Error getting account", t);
+                               setError(t);
                 if (t instanceof RestException)
                     displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
                 else
@@ -625,13 +624,14 @@ public class Pithos implements EntryPoint, ResizeHandler {
        HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
 
                        @Override
-                       public void onSuccess(@SuppressWarnings("unused") AccountResource _result) {
+                       public void onSuccess(AccountResource _result) {
                                showStatistics();
                        }
 
                        @Override
                        public void onError(Throwable t) {
                 GWT.log("Error getting account", t);
+                               setError(t);
                 if (t instanceof RestException)
                     displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
                 else
@@ -658,7 +658,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
         String path = "/" + Pithos.HOME_CONTAINER;
         PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
             @Override
-            public void onSuccess(@SuppressWarnings("unused") Resource result) {
+            public void onSuccess(Resource result) {
                if (!_account.hasTrashContainer())
                        createTrashContainer(callback);
                else
@@ -668,6 +668,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
             @Override
             public void onError(Throwable t) {
                 GWT.log("Error creating pithos", t);
+                               setError(t);
                 if (t instanceof RestException)
                     displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
                 else
@@ -687,13 +688,14 @@ public class Pithos implements EntryPoint, ResizeHandler {
         String path = "/" + Pithos.TRASH_CONTAINER;
         PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
             @Override
-            public void onSuccess(@SuppressWarnings("unused") Resource result) {
+            public void onSuccess(Resource result) {
                        fetchAccount(callback);
             }
 
             @Override
             public void onError(Throwable t) {
                 GWT.log("Error creating pithos", t);
+                               setError(t);
                 if (t instanceof RestException)
                     displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
                 else
@@ -840,7 +842,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
         try {
             builder.sendRequest("", new RequestCallback() {
                 @Override
-                public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
+                public void onResponseReceived(Request request, Response response) {
                     if (response.getStatusCode() == Response.SC_OK) {
                         JSONValue json = JSONParser.parseStrict(response.getText());
                         JSONArray array = json.isArray();
@@ -852,7 +854,8 @@ public class Pithos implements EntryPoint, ResizeHandler {
                 }
 
                 @Override
-                public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
+                public void onError(Request request, Throwable exception) {
+                       setError(exception);
                     displayError("System error unable to delete folder: " + exception.getMessage());
                 }
             });
@@ -867,15 +870,16 @@ public class Pithos implements EntryPoint, ResizeHandler {
             if (o != null && !o.containsKey("subdir")) {
                 JSONString name = o.get("name").isString();
                 String path = "/" + folder.getContainer() + "/" + name.stringValue();
-                DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), path) {
+                DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), URL.encode(path)) {
                     @Override
-                    public void onSuccess(@SuppressWarnings("unused") Resource result) {
+                    public void onSuccess(Resource result) {
                         deleteObject(folder, i + 1, array);
                     }
 
                     @Override
                     public void onError(Throwable t) {
                         GWT.log("", t);
+                                               setError(t);
                         displayError("System error unable to delete folder: " + t.getMessage());
                     }
 
@@ -896,7 +900,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
                 try {
                     builder.sendRequest("", new RequestCallback() {
                         @Override
-                        public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
+                        public void onResponseReceived(Request request, Response response) {
                             if (response.getStatusCode() == Response.SC_OK) {
                                 JSONValue json = JSONParser.parseStrict(response.getText());
                                 JSONArray array2 = json.isArray();
@@ -911,7 +915,8 @@ public class Pithos implements EntryPoint, ResizeHandler {
                         }
 
                         @Override
-                        public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
+                        public void onError(Request request, Throwable exception) {
+                               setError(exception);
                             displayError("System error unable to delete folder: " + exception.getMessage());
                         }
                     });
@@ -922,9 +927,9 @@ public class Pithos implements EntryPoint, ResizeHandler {
         }
         else {
             String path = folder.getUri();
-            DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
+            DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), URL.encode(path)) {
                 @Override
-                public void onSuccess(@SuppressWarnings("unused") Resource result) {
+                public void onSuccess(Resource result) {
                     updateFolder(folder.getParent(), true, new Command() {
                                                
                                                @Override
@@ -937,6 +942,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
                 @Override
                 public void onError(Throwable t) {
                     GWT.log("", t);
+                                       setError(t);
                     if (t instanceof RestException) {
                        if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
                                displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
@@ -967,13 +973,14 @@ public class Pithos implements EntryPoint, ResizeHandler {
             String path = targetUri + "/" + file.getName();
             PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
                 @Override
-                public void onSuccess(@SuppressWarnings("unused") Resource result) {
+                public void onSuccess(Resource result) {
                     copyFiles(iter, targetUsername, targetUri, callback);
                 }
 
                 @Override
                 public void onError(Throwable t) {
                     GWT.log("", t);
+                                       setError(t);
                     if (t instanceof RestException) {
                         displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
                     }
@@ -987,9 +994,9 @@ public class Pithos implements EntryPoint, ResizeHandler {
                                }
             };
             copyFile.setHeader("X-Auth-Token", getToken());
-            copyFile.setHeader("X-Copy-From", file.getUri());
+            copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
             if (!file.getOwner().equals(targetUsername))
-               copyFile.setHeader("X-Source-Account", file.getOwner());
+               copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwner()));
             copyFile.setHeader("Content-Type", file.getContentType());
             Scheduler.get().scheduleDeferred(copyFile);
         }
@@ -1018,7 +1025,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
         String path = targetUri + "/" + f.getName();
         PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
             @Override
-            public void onSuccess(@SuppressWarnings("unused") Resource result) {
+            public void onSuccess(Resource result) {
                GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
 
                                        @Override
@@ -1036,6 +1043,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
                                        @Override
                                        public void onError(Throwable t) {
                                GWT.log("", t);
+                                               setError(t);
                                if (t instanceof RestException) {
                                    displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
                                }
@@ -1055,7 +1063,8 @@ public class Pithos implements EntryPoint, ResizeHandler {
             @Override
             public void onError(Throwable t) {
                 GWT.log("", t);
-                if (t instanceof RestException) {
+                               setError(t);
+               if (t instanceof RestException) {
                     displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
                 }
                 else
@@ -1129,12 +1138,13 @@ public class Pithos implements EntryPoint, ResizeHandler {
                mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
                mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
                    @Override
-                   public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
+                   public void onSelectionChange(SelectionChangeEvent event) {
                        if (mysharedTreeSelectionModel.getSelectedObject() != null) {
                            deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
                            upload.setEnabled(false);
                            updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
                        }
+                       showRelevantToolbarButtons();
                    }
                });
                selectionModels.add(mysharedTreeSelectionModel);
@@ -1155,13 +1165,14 @@ public class Pithos implements EntryPoint, ResizeHandler {
                otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
                otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
                    @Override
-                   public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
+                   public void onSelectionChange(SelectionChangeEvent event) {
                        if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
                            deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
                            otherSharedTreeView.addStyleName("cellTreeWidget-selectedTree");
                            applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
                            updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
                        }
+                       showRelevantToolbarButtons();
                    }
                });
                selectionModels.add(otherSharedTreeSelectionModel);
@@ -1178,12 +1189,45 @@ public class Pithos implements EntryPoint, ResizeHandler {
        }
 
        public void logoff() {
-        Configuration conf = (Configuration) GWT.create(Configuration.class);
-               Cookies.removeCookie(conf.authCookie(), "/");
-               Cookies.removeCookie(conf.authTokenCookie(), "/");
+               Cookies.removeCookie(config.authCookie(), "/");
+               Cookies.removeCookie(config.authTokenCookie(), "/");
                for (String s: Cookies.getCookieNames())
-                       if (s.startsWith(conf.shibSessionCookiePrefix()))
+                       if (s.startsWith(config.shibSessionCookiePrefix()))
                                Cookies.removeCookie(s, "/");
-               Window.Location.assign(Window.Location.getPath());
+               Window.Location.assign(config.logoutUrl());
+       }
+       
+       public native void log1(String message)/*-{
+               $wnd.console.log(message);
+       }-*/;
+
+       public String getErrorData() {
+               if (error != null)
+                       return error.toString();
+               return "";
+       }
+       
+       public void setError(Throwable t) {
+               error = t;
+       }
+       
+       public void showRelevantToolbarButtons() {
+               toolbar.showRelevantButtons();
+       }
+
+       public FileUploadDialog getFileUploadDialog() {
+               if (fileUploadDialog == null)
+                       fileUploadDialog = new FileUploadDialog(this);
+               return fileUploadDialog;
+       }
+
+       public void hideUploadIndicator() {
+               upload.removeStyleName("pithos-uploadButton-loading");
+               upload.setTitle("");
+       }
+       
+       public void showUploadIndicator() {
+               upload.addStyleName("pithos-uploadButton-loading");
+               upload.setTitle("Upload in progress. Click for details.");
        }
 }