Revert "Work in progress. Do not rely on this commit"
[pithos-web-client] / src / gr / grnet / pithos / web / client / commands / PasteCommand.java
index 58eacf4..1f1c486 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
@@ -36,10 +36,10 @@ package gr.grnet.pithos.web.client.commands;
 
 import com.google.gwt.core.client.Scheduler;
 import gr.grnet.pithos.web.client.Clipboard;
-import gr.grnet.pithos.web.client.GSS;
+import gr.grnet.pithos.web.client.Pithos;
 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.Resource;
 import gr.grnet.pithos.web.client.rest.PutRequest;
 import gr.grnet.pithos.web.client.rest.RestException;
 
@@ -47,16 +47,18 @@ import java.util.Iterator;
 import java.util.List;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.http.client.Response;
+import com.google.gwt.http.client.URL;
 import com.google.gwt.user.client.Command;
 import com.google.gwt.user.client.ui.PopupPanel;
 
 public class PasteCommand implements Command {
 
-    private GSS app;
+       protected Pithos app;
        private PopupPanel containerPanel;
-    private Folder folder;
+       protected Folder folder;
 
-       public PasteCommand(GSS _app, PopupPanel _containerPanel, Folder _folder) {
+       public PasteCommand(Pithos _app, PopupPanel _containerPanel, Folder _folder) {
         app = _app;
                containerPanel = _containerPanel;
         folder = _folder;
@@ -64,88 +66,106 @@ public class PasteCommand implements Command {
 
        @Override
        public void execute() {
-               containerPanel.hide();
+        if (containerPanel != null)
+               containerPanel.hide();
+
         Object clipboardItem = app.getClipboard().getItem();
         if (clipboardItem == null)
             return;
         int operation = app.getClipboard().getOperation();
         if (clipboardItem instanceof Folder) {
+            final Folder tobeCopied = (Folder) clipboardItem;
             if (operation == Clipboard.COPY) {
-
+               String targetUri = folder.getUri() + "/" + tobeCopied.getName();
+                app.copyFolder(tobeCopied, folder.getOwnerID(), targetUri, false, new Command() {
+                    @Override
+                    public void execute() {
+                        app.getClipboard().clear();
+                        app.updateFolder(folder, true, new Command() {
+                                                       
+                                                       @Override
+                                                       public void execute() {
+                                                               app.updateStatistics();
+                                                       }
+                                               }, true);
+                    }
+                });
             }
             else {
-
+               String targetUri = folder.getUri() + "/" + tobeCopied.getName();
+                app.copyFolder(tobeCopied, folder.getOwnerID(), targetUri, true, new Command() {
+                    @Override
+                    public void execute() {
+                        app.getClipboard().clear();
+                        app.updateFolder(folder, true, null, true);
+                    }
+                });
             }
         }
         else {
-            List<File> tobeCopied = (List<File>) clipboardItem;
+            @SuppressWarnings("unchecked")
+                       List<File> tobeCopied = (List<File>) clipboardItem;
             Iterator<File> iter = tobeCopied.iterator();
             if (operation == Clipboard.COPY) {
-                copyFiles(iter);
+                app.copyFiles(iter, folder.getOwnerID(), folder.getUri(), new Command() {
+                    @Override
+                    public void execute() {
+                        app.getClipboard().clear();
+                        app.updateFolder(folder, true, new Command() {
+                                                       
+                                                       @Override
+                                                       public void execute() {
+                                                               app.updateStatistics();
+                                                       }
+                                               }, true);
+                    }
+                });
             }
             else {
-                moveFiles(iter);
+                moveFiles(iter, new Command() {
+                    @Override
+                    public void execute() {
+                        app.getClipboard().clear();
+                        app.updateFolder(folder, true, null, true);
+                    }
+                });
             }
         }
        }
 
-    private void moveFiles(final Iterator<File> iter) {
+       protected void moveFiles(final Iterator<File> iter, final Command callback) {
         if (iter.hasNext()) {
             File file = iter.next();
-            String path = app.getApiPath() + app.getUsername() + folder.getUri() + "/" + file.getName();
-            PutRequest copyFile = new PutRequest(path) {
+            String path = folder.getUri() + "/" + file.getName();
+            PutRequest copyFile = new PutRequest(app.getApiPath(), folder.getOwnerID(), path) {
                 @Override
                 public void onSuccess(Resource result) {
-                    moveFiles(iter);
+                    moveFiles(iter, callback);
                 }
 
                 @Override
                 public void onError(Throwable t) {
                     GWT.log("", t);
+                                       app.setError(t);
                     if (t instanceof RestException) {
-                        GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
+                        app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
                     }
                     else
-                        GSS.get().displayError("System error unable to copy file: "+t.getMessage());
+                        app.displayError("System error unable to copy file: "+t.getMessage());
                 }
-            };
-            copyFile.setHeader("X-Auth-Token", app.getToken());
-            copyFile.setHeader("X-Move-From", file.getUri());
-            Scheduler.get().scheduleDeferred(copyFile);
-        }
-        else {
-            app.getClipboard().clear();
-            app.updateFolder(folder);
-        }
-    }
 
-    private void copyFiles(final Iterator<File> iter) {
-        if (iter.hasNext()) {
-            File file = iter.next();
-            String path = app.getApiPath() + app.getUsername() + folder.getUri() + "/" + file.getName();
-            PutRequest copyFile = new PutRequest(path) {
-                @Override
-                public void onSuccess(Resource result) {
-                    copyFiles(iter);
-                }
-
-                @Override
-                public void onError(Throwable t) {
-                    GWT.log("", t);
-                    if (t instanceof RestException) {
-                        GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
-                    }
-                    else
-                        GSS.get().displayError("System error unable to copy file: "+t.getMessage());
-                }
+                               @Override
+                               protected void onUnauthorized(Response response) {
+                                       app.sessionExpired();
+                               }
             };
-            copyFile.setHeader("X-Auth-Token", app.getToken());
-            copyFile.setHeader("X-Copy-From", file.getUri());
+            copyFile.setHeader("X-Auth-Token", app.getUserToken());
+            copyFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
+            copyFile.setHeader("Content-Type", file.getContentType());
             Scheduler.get().scheduleDeferred(copyFile);
         }
-        else {
-            app.getClipboard().clear();
-            app.updateFolder(folder);
+        else if (callback != null) {
+            callback.execute();
         }
     }
 }