Implemented trash without restore
authorChristos Stathis <chstath@ebs.gr>
Fri, 9 Sep 2011 15:05:08 +0000 (18:05 +0300)
committerChristos Stathis <chstath@ebs.gr>
Fri, 9 Sep 2011 15:05:08 +0000 (18:05 +0300)
src/gr/grnet/pithos/web/client/FileList.java
src/gr/grnet/pithos/web/client/FileUploadDialog.java
src/gr/grnet/pithos/web/client/Pithos.java
src/gr/grnet/pithos/web/client/commands/ToTrashCommand.java
src/gr/grnet/pithos/web/client/foldertree/AccountResource.java
src/gr/grnet/pithos/web/client/foldertree/File.java
src/gr/grnet/pithos/web/client/foldertree/Folder.java
src/gr/grnet/pithos/web/client/foldertree/FolderTreeViewModel.java
src/gr/grnet/pithos/web/client/mysharedtree/MysharedTreeView.java
src/gr/grnet/pithos/web/client/othersharedtree/OtherSharedTreeView.java
src/gr/grnet/pithos/web/client/tagtree/Tag.java

index 7031544..91310e2 100644 (file)
@@ -430,8 +430,7 @@ public class FileList extends Composite {
        public void setFiles(final List<File> _files) {
                files = new ArrayList<File>();
        for (File fres : _files)
-               if (!fres.isInTrash())
-                               files.add(fres);
+                       files.add(fres);
                Collections.sort(files, new Comparator<File>() {
 
                        @Override
index 45a6e33..979d57b 100644 (file)
@@ -257,7 +257,7 @@ 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;
        }
index f9421a5..6907b0d 100644 (file)
@@ -104,6 +104,8 @@ import com.google.gwt.view.client.SingleSelectionModel;
 public class Pithos implements EntryPoint, ResizeHandler {
 
        public static final String HOME_CONTAINER = "pithos";
+
+       public static final String TRASH_CONTAINER = "trash";
        
        /**
         * Instantiate an application-level image bundle. This object will provide
@@ -503,8 +505,10 @@ public class Pithos implements EntryPoint, ResizeHandler {
             @Override
             public void onSuccess(AccountResource _result) {
                 account = _result;
-                if (account.getContainers().isEmpty())
-                    createHomeContainers();
+                if (!account.hasHomeContainer())
+                    createHomeContainer(account);
+                else if (!account.hasTrashContainer())
+                       createTrashContainer();
                 else {
                     folderTreeViewModel.initialize(account);
                 }
@@ -523,12 +527,15 @@ public class Pithos implements EntryPoint, ResizeHandler {
         Scheduler.get().scheduleDeferred(getAccount);
     }
 
-    protected void createHomeContainers() {
-        String path = "/pithos";
+    protected void createHomeContainer(final AccountResource account) {
+        String path = "/" + Pithos.HOME_CONTAINER;
         PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
             @Override
             public void onSuccess(@SuppressWarnings("unused") Resource result) {
-                fetchAccount();
+               if (!account.hasTrashContainer())
+                       createTrashContainer();
+               else
+                       fetchAccount();
             }
 
             @Override
@@ -544,7 +551,28 @@ public class Pithos implements EntryPoint, ResizeHandler {
         Scheduler.get().scheduleDeferred(createPithos);
     }
 
-       /**
+    protected void createTrashContainer() {
+        String path = "/" + Pithos.TRASH_CONTAINER;
+        PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
+            @Override
+            public void onSuccess(@SuppressWarnings("unused") Resource result) {
+                       fetchAccount();
+            }
+
+            @Override
+            public void onError(Throwable t) {
+                GWT.log("Error creating pithos", t);
+                if (t instanceof RestException)
+                    displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
+                else
+                    displayError("System error Error creating pithos: " + t.getMessage());
+            }
+        };
+        createPithos.setHeader("X-Auth-Token", getToken());
+        Scheduler.get().scheduleDeferred(createPithos);
+    }
+
+    /**
         * Creates an HTML fragment that places an image & caption together, for use
         * in a group header.
         *
@@ -768,7 +796,10 @@ public class Pithos implements EntryPoint, ResizeHandler {
                 public void onError(Throwable t) {
                     GWT.log("", t);
                     if (t instanceof RestException) {
-                        displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
+                       if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
+                               displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
+                       else
+                               onSuccess(null);
                     }
                     else
                         displayError("System error unable to delete folder: " + t.getMessage());
index c28b01b..9cec918 100644 (file)
@@ -40,6 +40,7 @@ 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.PostRequest;
+import gr.grnet.pithos.web.client.rest.PutRequest;
 import gr.grnet.pithos.web.client.rest.RestException;
 
 import java.util.Iterator;
@@ -95,8 +96,8 @@ public class ToTrashCommand implements Command{
        }
 
     private void trashFolder(final Folder f, final Command callback) {
-        String path = f.getUri() + "?update=";
-        PostRequest trashFolder = new PostRequest(app.getApiPath(), app.getUsername(), path) {
+        String path = "/" + Pithos.TRASH_CONTAINER + "/" + f.getPrefix() + "/" + f.getName();
+        PutRequest createFolder = new PutRequest(app.getApiPath(), app.getUsername(), path) {
             @Override
             public void onSuccess(@SuppressWarnings("unused") Resource result) {
                 Iterator<File> iter = f.getFiles().iterator();
@@ -104,12 +105,7 @@ public class ToTrashCommand implements Command{
                     @Override
                     public void execute() {
                         Iterator<Folder> iterf = f.getSubfolders().iterator();
-                        trashSubfolders(iterf, new Command() {
-                            @Override
-                            public void execute() {
-                                callback.execute();
-                            }
-                        });
+                        trashSubfolders(iterf, callback);
                     }
                 });
             }
@@ -124,16 +120,18 @@ public class ToTrashCommand implements Command{
                     app.displayError("System error creating folder:" + t.getMessage());
             }
         };
-        trashFolder.setHeader("X-Auth-Token", app.getToken());
-        trashFolder.setHeader("X-Object-Meta-Trash", "true");
-        Scheduler.get().scheduleDeferred(trashFolder);
+        createFolder.setHeader("X-Auth-Token", app.getToken());
+        createFolder.setHeader("Accept", "*/*");
+        createFolder.setHeader("Content-Length", "0");
+        createFolder.setHeader("Content-Type", "application/folder");
+        Scheduler.get().scheduleDeferred(createFolder);
     }
 
     protected void trashFiles(final Iterator<File> iter, final Command callback) {
         if (iter.hasNext()) {
             File file = iter.next();
-            String path = file.getUri() + "?update=";
-            PostRequest trashFile = new PostRequest(app.getApiPath(), app.getUsername(), path) {
+            String path = "/" + Pithos.TRASH_CONTAINER + "/" + file.getPath();
+            PutRequest trashFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
                 @Override
                 public void onSuccess(@SuppressWarnings("unused") Resource result) {
                     trashFiles(iter, callback);
@@ -150,10 +148,10 @@ public class ToTrashCommand implements Command{
                 }
             };
             trashFile.setHeader("X-Auth-Token", app.getToken());
-            trashFile.setHeader("X-Object-Meta-Trash", "true");
+            trashFile.setHeader("X-Move-From", file.getUri());
             Scheduler.get().scheduleDeferred(trashFile);
         }
-        else  if (callback != null) {
+        else if (callback != null) {
             callback.execute();
         }
     }
index 4bec6c9..6a83fd8 100644 (file)
@@ -35,6 +35,8 @@
 
 package gr.grnet.pithos.web.client.foldertree;
 
+import gr.grnet.pithos.web.client.Pithos;
+
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -214,4 +216,18 @@ public class AccountResource extends Resource {
     public List<Group> getGroups() {
         return groups;
     }
+    
+    public boolean hasHomeContainer() {
+       for (Folder f : containers)
+               if (f.getName().equals(Pithos.HOME_CONTAINER))
+                       return true;
+       return false;
+    }
+
+    public boolean hasTrashContainer() {
+       for (Folder f : containers)
+               if (f.getName().equals(Pithos.TRASH_CONTAINER))
+                       return true;
+       return false;
+    }
 }
index 10ee71b..b3559e4 100644 (file)
@@ -39,6 +39,9 @@ import com.google.gwt.http.client.Header;
 import com.google.gwt.http.client.Response;
 import com.google.gwt.i18n.client.NumberFormat;
 import com.google.gwt.json.client.JSONObject;
+
+import gr.grnet.pithos.web.client.Pithos;
+
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -66,8 +69,6 @@ public class File extends Resource {
 
     private String owner;
 
-    private boolean inTrash;
-
     private String container;
 
     private Folder parent;
@@ -141,10 +142,6 @@ public class File extends Resource {
         return !permissions.isEmpty();
     }
 
-    public boolean isInTrash() {
-        return inTrash;
-    }
-
     public void populate(Folder _parent, JSONObject o, String _owner, String _container) {
         this.parent = _parent;
         path = unmarshallString(o, "name");
@@ -170,7 +167,7 @@ public class File extends Resource {
             parsePermissions(rawPermissions);
 
         for (String key : o.keySet())
-            if (key.startsWith("x_object_meta_") && !key.equals("x_object_meta_trash"))
+            if (key.startsWith("x_object_meta_"))
                 tags.add(key.substring("x_object_meta_".length()).trim().toLowerCase());
 
         
@@ -226,7 +223,7 @@ public class File extends Resource {
         this.owner = _owner;
         for (Header h : response.getHeaders()) {
             String header = h.getName();
-            if (header.startsWith("X-Object-Meta-") && !header.equals("X-Object-Meta-Trash"))
+            if (header.startsWith("X-Object-Meta-"))
                 tags.add(header.substring("X-Object-Meta-".length()).trim().toLowerCase());
             else if (header.equals("X-Object-Sharing")) {
                 String rawPermissions = h.getValue();
@@ -236,11 +233,6 @@ public class File extends Resource {
                 inheritedPermissionsFrom = h.getValue().trim();
             }
         }
-        String header = response.getHeader("X-Object-Meta-Trash");
-        if (header != null)
-            inTrash = Boolean.valueOf(header);
-        else
-            inTrash = false;
     }
 
     public Folder getParent() {
index 4098189..202408e 100644 (file)
@@ -35,6 +35,8 @@
 
 package gr.grnet.pithos.web.client.foldertree;
 
+import gr.grnet.pithos.web.client.Pithos;
+
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -77,13 +79,6 @@ public class Folder extends Resource {
 
     private Set<File> files = new LinkedHashSet<File>();
 
-    private boolean inTrash = false;
-
-    /*
-     * Flag that indicates that this folder is the Trash
-     */
-    private boolean trash = false;
-
     private Set<String> tags = new LinkedHashSet<String>();
 
     private String owner;
@@ -167,10 +162,6 @@ public class Folder extends Resource {
         if (header != null)
             bytesUsed = Long.valueOf(header);
 
-        header = response.getHeader("X-Object-Meta-Trash");
-        if (header != null && header.equals("true"))
-            inTrash = true;
-
         header = response.getHeader("X-Container-Object-Meta");
         if (header != null && header.length() > 0) {
             for (String t : header.split(",")) {
@@ -197,21 +188,13 @@ public class Folder extends Resource {
                         f.populate(this, o, _owner, container);
                         subfolders.add(f);
                     }
-                    else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) {
+                    else {
                         File file = new File();
                         file.populate(this, o, _owner, container);
                         files.add(file);
                     }
                 }
             }
-            //This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
-            //avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
-            Iterator<Folder> iter = subfolders.iterator();
-            while (iter.hasNext()) {
-                Folder f = iter.next();
-                if (f.isInTrash())
-                    iter.remove();
-            }
         }
     }
 
@@ -240,8 +223,6 @@ public class Folder extends Resource {
             prefix = "";
         }
         this.owner = _owner;
-        if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))
-            inTrash = true;
 
         inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
         String rawPermissions = unmarshallString(o, "x_object_sharing");
@@ -286,22 +267,10 @@ public class Folder extends Resource {
         return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
     }
 
-    public boolean isInTrash() {
-        return inTrash;
-    }
-
     public boolean isContainer() {
         return parent == null;
     }
 
-    public boolean isTrash() {
-        return trash;
-    }
-
-    public void setTrash(boolean trash) {
-        this.trash = trash;
-    }
-
     public void setContainer(String container) {
         this.container = container;
     }
@@ -336,4 +305,12 @@ public class Folder extends Resource {
        public boolean isShared() {
                return !permissions.isEmpty();
        }
+
+       public boolean isTrash() {
+               return isContainer() && name.equals(Pithos.TRASH_CONTAINER);
+       }
+
+       public boolean isHome() {
+               return isContainer() && name.equals(Pithos.HOME_CONTAINER);
+       }
 }
index ac8a776..ea41fc3 100644 (file)
@@ -51,6 +51,7 @@ import com.google.gwt.core.client.GWT;
 import com.google.gwt.core.client.Scheduler;
 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
 import com.google.gwt.event.dom.client.ContextMenuEvent;
+import com.google.gwt.safehtml.shared.SafeHtml;
 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
 import com.google.gwt.user.client.Command;
 import com.google.gwt.user.client.ui.AbstractImagePrototype;
@@ -67,12 +68,21 @@ public class FolderTreeViewModel implements TreeViewModel {
        @Override
         public void render(@SuppressWarnings("unused") Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
             String html;
-            if (folder.isTrash())
-                html = AbstractImagePrototype.create(FolderTreeView.images.trash()).getHTML();
-            else
-                html = AbstractImagePrototype.create(FolderTreeView.images.folderYellow()).getHTML();
+            SafeHtml name;
+               if (folder.isHome()) {
+                       html = AbstractImagePrototype.create(FolderTreeView.images.home()).getHTML();
+                       name = Templates.INSTANCE.nameSpan("Home");
+               }
+               else if (folder.isTrash()) {
+                       html = AbstractImagePrototype.create(FolderTreeView.images.trash()).getHTML();
+                       name = Templates.INSTANCE.nameSpan("Trash");
+               }
+            else {
+               html = AbstractImagePrototype.create(FolderTreeView.images.folderYellow()).getHTML();
+                       name = Templates.INSTANCE.nameSpan(folder.getName());
+            }
             safeHtmlBuilder.appendHtmlConstant(html);
-            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
+            safeHtmlBuilder.append(name);
         }
 
         @Override
@@ -159,11 +169,6 @@ public class FolderTreeViewModel implements TreeViewModel {
                 rootDataProvider.getList().clear();
                 rootDataProvider.getList().addAll(account.getContainers());
                 selectionModel.setSelected(rootDataProvider.getList().get(0), true);
-                Folder f = new Folder("Trash");
-                f.setTrash(true);
-                f.setContainer("trash");
-                rootDataProvider.getList().add(f);
-                app.updateTags();
             }
         });
     }
index 99aef4a..5537cc7 100644 (file)
@@ -112,9 +112,6 @@ public class MysharedTreeView extends Composite {
 
         @Source("gr/grnet/pithos/resources/folder_user.png")
         ImageResource sharedFolder();
-
-        @Source("gr/grnet/pithos/resources/trash.png")
-        ImageResource trash();
     }
 
     static Images images = GWT.create(Images.class);
index a089d1f..531510e 100644 (file)
@@ -113,9 +113,6 @@ public class OtherSharedTreeView extends Composite {
         @Source("gr/grnet/pithos/resources/folder_user.png")
         ImageResource sharedFolder();
 
-        @Source("gr/grnet/pithos/resources/trash.png")
-        ImageResource trash();
-
         @Source("gr/grnet/pithos/resources/edit_user.png")
         ImageResource user();
     }
index f8544f7..857dfb4 100644 (file)
@@ -68,10 +68,6 @@ public class Tag extends Resource {
 //        if (header != null)
 //            bytesUsed = Long.valueOf(header);
 //
-//        header = response.getHeader("X-Object-Meta-Trash");
-//        if (header != null && header.equals("true"))
-//            inTrash = true;
-//
 //        subfolders.clear(); //This is necessary in case we update a pre-existing Tag so that stale subfolders won't show up
 //        files.clear();
 //        JSONValue json = JSONParser.parseStrict(response.getText());
@@ -86,21 +82,13 @@ public class Tag extends Resource {
 //                        f.populate(this, o, container);
 //                        subfolders.add(f);
 //                    }
-//                    else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) {
+//                    else {
 //                        File file = new File();
 //                        file.populate(this, o, container);
 //                        files.add(file);
 //                    }
 //                }
 //            }
-//            //This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
-//            //avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
-//            Iterator<Tag> iter = subfolders.iterator();
-//            while (iter.hasNext()) {
-//                Tag f = iter.next();
-//                if (f.isInTrash())
-//                    iter.remove();
-//            }
 //        }
 //    }
 //
@@ -128,8 +116,6 @@ public class Tag extends Resource {
 //            container = name;
 //            prefix = "";
 //        }
-//        if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))
-//            inTrash = true;
 //    }
 //
 //    public static Tag createFromResponse(Response response, Tag result) {