Fixed various compilation warnings
[pithos-web-client] / src / gr / grnet / pithos / web / client / FilePropertiesDialog.java
index a1ae971..473824c 100644 (file)
  */\r
 package gr.grnet.pithos.web.client;\r
 \r
-import com.google.gwt.core.client.Scheduler;\r
-import com.google.gwt.json.client.JSONArray;\r
-import com.google.gwt.json.client.JSONString;\r
-import com.google.gwt.user.client.Window;\r
-import com.google.gwt.user.client.ui.Anchor;\r
-import com.google.gwt.user.client.ui.Label;\r
 import gr.grnet.pithos.web.client.foldertree.File;\r
 import gr.grnet.pithos.web.client.foldertree.Resource;\r
-import gr.grnet.pithos.web.client.rest.PostCommand;\r
 import gr.grnet.pithos.web.client.rest.PostRequest;\r
 import gr.grnet.pithos.web.client.rest.PutRequest;\r
-import gr.grnet.pithos.web.client.rest.RestException;\r
+import gr.grnet.pithos.web.client.tagtree.Tag;\r
+\r
+import java.util.Map;\r
 \r
 import com.google.gwt.core.client.GWT;\r
+import com.google.gwt.core.client.Scheduler;\r
 import com.google.gwt.event.dom.client.ClickEvent;\r
 import com.google.gwt.event.dom.client.ClickHandler;\r
 import com.google.gwt.i18n.client.DateTimeFormat;\r
-import com.google.gwt.json.client.JSONBoolean;\r
-import com.google.gwt.json.client.JSONObject;\r
-import com.google.gwt.resources.client.ClientBundle;\r
 import com.google.gwt.resources.client.ImageResource;\r
-import com.google.gwt.user.client.Command;\r
-import com.google.gwt.user.client.DeferredCommand;\r
+import com.google.gwt.user.client.Window;\r
+import com.google.gwt.user.client.ui.Anchor;\r
 import com.google.gwt.user.client.ui.Button;\r
 import com.google.gwt.user.client.ui.CheckBox;\r
 import com.google.gwt.user.client.ui.DecoratedTabPanel;\r
@@ -66,10 +59,9 @@ import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.FocusPanel;\r
 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
 import com.google.gwt.user.client.ui.HorizontalPanel;\r
+import com.google.gwt.user.client.ui.Label;\r
 import com.google.gwt.user.client.ui.TextBox;\r
 import com.google.gwt.user.client.ui.VerticalPanel;\r
-import gr.grnet.pithos.web.client.tagtree.Tag;\r
-import java.util.Iterator;\r
 \r
 /**\r
  * The 'File properties' dialog box implementation.\r
@@ -77,14 +69,14 @@ import java.util.Iterator;
  */\r
 public class FilePropertiesDialog extends AbstractPropertiesDialog {\r
 \r
-       private PermissionsList permList;\r
+       protected PermissionsList permList;\r
 \r
-       private CheckBox readForAll;\r
+       protected CheckBox readForAll;\r
 \r
        /**\r
         * An image bundle for this widgets images.\r
         */\r
-       public interface Images extends ClientBundle,MessagePanel.Images {\r
+       public interface Images extends MessagePanel.Images {\r
 \r
                @Source("gr/grnet/pithos/resources/edit_user.png")\r
                ImageResource permUser();\r
@@ -107,12 +99,8 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
         */\r
        private TextBox name = new TextBox();\r
 \r
-       private final CheckBox versioned = new CheckBox();\r
-\r
        final File file;\r
 \r
-       private String userFullName;\r
-\r
     Images images = GWT.create(Images.class);\r
 \r
        /**\r
@@ -148,7 +136,7 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
                // when the button is clicked.\r
                final Button ok = new Button("OK", new ClickHandler() {\r
                        @Override\r
-                       public void onClick(ClickEvent event) {\r
+                       public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
                                accept();\r
                                closeDialog();\r
                        }\r
@@ -160,7 +148,7 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
                // dialog when the button is clicked.\r
                final Button cancel = new Button("Cancel", new ClickHandler() {\r
                        @Override\r
-                       public void onClick(ClickEvent event) {\r
+                       public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
                                closeDialog();\r
                        }\r
                });\r
@@ -235,7 +223,7 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
             tagAnchor.addClickHandler(new ClickHandler() {\r
 \r
                 @Override\r
-                public void onClick(ClickEvent event) {\r
+                public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
                     String existing = tags.getText().trim();\r
                     if (MULTIPLE_VALUES_TEXT.equals(existing))\r
                         existing = "";\r
@@ -256,35 +244,37 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
     private VerticalPanel createSharingPanel() {\r
         VerticalPanel permPanel = new VerticalPanel();\r
 \r
-        permList = new PermissionsList(app, images, file.getPermissions(), file.getOwner());\r
+        permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);\r
         permPanel.add(permList);\r
 \r
-        HorizontalPanel permButtons = new HorizontalPanel();\r
-        Button add = new Button("Add Group", new ClickHandler() {\r
-            @Override\r
-            public void onClick(ClickEvent event) {\r
-                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);\r
-                dlg.center();\r
-                permList.updatePermissionTable();\r
-            }\r
-        });\r
-        permButtons.add(add);\r
-        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);\r
+        if (file.getInheritedPermissionsFrom() == null) {\r
+            HorizontalPanel permButtons = new HorizontalPanel();\r
+            Button add = new Button("Add Group", new ClickHandler() {\r
+                @Override\r
+                public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
+                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);\r
+                    dlg.center();\r
+                    permList.updatePermissionTable();\r
+                }\r
+            });\r
+            permButtons.add(add);\r
+            permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);\r
 \r
-        final Button addUser = new Button("Add User", new ClickHandler() {\r
-            @Override\r
-            public void onClick(ClickEvent event) {\r
-                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);\r
-                dlg.center();\r
-                permList.updatePermissionTable();\r
-            }\r
-        });\r
-        permButtons.add(addUser);\r
-        permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);\r
+            final Button addUser = new Button("Add User", new ClickHandler() {\r
+                @Override\r
+                public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
+                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);\r
+                    dlg.center();\r
+                    permList.updatePermissionTable();\r
+                }\r
+            });\r
+            permButtons.add(addUser);\r
+            permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);\r
 \r
-        permButtons.setSpacing(8);\r
-        permButtons.addStyleName("pithos-TabPanelBottom");\r
-        permPanel.add(permButtons);\r
+            permButtons.setSpacing(8);\r
+            permButtons.addStyleName("pithos-TabPanelBottom");\r
+            permPanel.add(permButtons);\r
+        }\r
 \r
         final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +\r
                     " by everyone. By checking this option, you are certifying that you have the right to " +\r
@@ -296,7 +286,7 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
         readForAll.setValue(file.isPublished());\r
         readForAll.addClickHandler(new ClickHandler() {\r
             @Override\r
-            public void onClick(ClickEvent event) {\r
+            public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
                 readForAllNote.setVisible(readForAll.getValue());\r
             }\r
         });\r
@@ -401,60 +391,31 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
        @Override\r
        protected void accept() {\r
                String newFilename = null;\r
-//             permList.updatePermissionsAccordingToInput();\r
-//             Set<PermissionHolder> perms = permList.getPermissions();\r
-//             JSONObject json = new JSONObject();\r
+\r
+               final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);\r
+\r
                if (!name.getText().trim().equals(file.getName())) {\r
                        newFilename = name.getText().trim();\r
-//                     json.put("name", new JSONString(newFilename));\r
                }\r
-//             if (versioned.getValue() != file.isVersioned())\r
-//                     json.put("versioned", JSONBoolean.getInstance(versioned.getValue()));\r
+\r
                //only update the read for all perm if the user is the owner\r
         Boolean published = null;\r
                if (readForAll.getValue() != file.isPublished())\r
                        if (file.getOwner().equals(app.getUsername()))\r
                 published = readForAll.getValue();\r
         final Boolean finalPublished = published;\r
-//                             json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));\r
-//             int i = 0;\r
-//             if (permList.hasChanges()) {\r
-//                     GWT.log("Permissions change", null);\r
-//                     JSONArray perma = new JSONArray();\r
-//\r
-//                     for (PermissionHolder p : perms) {\r
-//                             JSONObject po = new JSONObject();\r
-//                             if (p.getUser() != null)\r
-//                                     po.put("user", new JSONString(p.getUser()));\r
-//                             if (p.getGroup() != null)\r
-//                                     po.put("group", new JSONString(p.getGroup()));\r
-//                             po.put("read", JSONBoolean.getInstance(p.isRead()));\r
-//                             po.put("write", JSONBoolean.getInstance(p.isWrite()));\r
-//                             po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));\r
-//                             perma.set(i, po);\r
-//                             i++;\r
-//                     }\r
-//                     json.put("permissions", perma);\r
-//             }\r
+\r
         String[] tagset = null;\r
                if (!tags.getText().equals(initialTagText))\r
                        tagset = tags.getText().split(",");\r
-//             String jsonString = json.toString();\r
-//             if(jsonString.equals("{}")){\r
-//                     GWT.log("NO CHANGES", null);\r
-//                     return;\r
-//             }\r
         final String[] newTags = tagset;\r
 \r
         if (newFilename != null) {\r
             final String path = file.getParent().getUri() + "/" + newFilename;\r
             PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {\r
                 @Override\r
-                public void onSuccess(Resource result) {\r
-                    if (newTags != null || finalPublished != null)\r
-                        updateMetaData(app.getApiPath(), app.getUsername(), path + "?update=", newTags, finalPublished);\r
-                    else\r
-                        app.updateFolder(file.getParent());\r
+                public void onSuccess(@SuppressWarnings("unused") Resource result) {\r
+                    updateMetaData(app.getApiPath(), app.getUsername(), path + "?update=", newTags, finalPublished, perms);\r
                 }\r
 \r
                 @Override\r
@@ -468,96 +429,59 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
             updateFile.setHeader("Content-Type", file.getContentType());\r
             Scheduler.get().scheduleDeferred(updateFile);\r
         }\r
-        else if (newTags != null || finalPublished != null)\r
-            updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished);\r
+        else\r
+            updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished, perms);\r
        }\r
 \r
-    private void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published) {\r
-        PostRequest updateFile = new PostRequest(api, owner, path) {\r
-            @Override\r
-            public void onSuccess(Resource result) {\r
-                app.updateFolder(file.getParent());\r
-            }\r
+       protected void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published, Map<String, Boolean[]> newPermissions) {\r
+        if (newTags != null || published != null || newPermissions != null) {\r
+            PostRequest updateFile = new PostRequest(api, owner, path) {\r
+                @Override\r
+                public void onSuccess(@SuppressWarnings("unused") Resource result) {\r
+                    app.updateFolder(file.getParent(), true);\r
+                }\r
 \r
-            @Override\r
-            public void onError(Throwable t) {\r
-                GWT.log("", t);\r
-                app.displayError("System error modifying file:" + t.getMessage());\r
+                @Override\r
+                public void onError(Throwable t) {\r
+                    GWT.log("", t);\r
+                    app.displayError("System error modifying file:" + t.getMessage());\r
+                }\r
+            };\r
+            updateFile.setHeader("X-Auth-Token", app.getToken());\r
+            if (newTags != null)\r
+                for (String t : newTags)\r
+                    updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");\r
+            if (published != null)\r
+                updateFile.setHeader("X-Object-Public", published.toString());\r
+            if (newPermissions != null) {\r
+                String readPermHeader = "read=";\r
+                String writePermHeader = "write=";\r
+                for (String u : newPermissions.keySet()) {\r
+                    Boolean[] p = newPermissions.get(u);\r
+                    if (p[0] != null && p[0])\r
+                        readPermHeader += u + ",";\r
+                    if (p[1] != null && p[1])\r
+                        writePermHeader += u + ",";\r
+                }\r
+                if (readPermHeader.endsWith("="))\r
+                    readPermHeader = "";\r
+                else if (readPermHeader.endsWith(","))\r
+                    readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);\r
+                if (writePermHeader.endsWith("="))\r
+                    writePermHeader = "";\r
+                else if (writePermHeader.endsWith(","))\r
+                    writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);\r
+                String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;\r
+                if (permHeader.length() == 0)\r
+                    permHeader="~";\r
+                updateFile.setHeader("X-Object-Sharing", permHeader);\r
             }\r
-        };\r
-        updateFile.setHeader("X-Auth-Token", app.getToken());\r
-        if (newTags != null)\r
-            for (String t : newTags)\r
-                updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");\r
-        if (published != null)\r
-            updateFile.setHeader("X-Object-Public", published.toString());\r
-        Scheduler.get().scheduleDeferred(updateFile);\r
+            Scheduler.get().scheduleDeferred(updateFile);\r
+        }\r
+        else\r
+            app.updateFolder(file.getParent(), true);\r
     }\r
 \r
        private void removeAllOldVersions() {\r
-               JSONObject json = new JSONObject();\r
-               json.put("versioned", JSONBoolean.getInstance(false));\r
-               GWT.log(json.toString(), null);\r
-               PostCommand cf = new PostCommand(app, file.getUri() + "?update=", json.toString(), 200) {\r
-\r
-                       @Override\r
-                       public void onComplete() {\r
-                               toggleVersioned(true);\r
-                       }\r
-\r
-                       @Override\r
-                       public void onError(Throwable t) {\r
-                               GWT.log("", t);\r
-                               if (t instanceof RestException) {\r
-                                       int statusCode = ((RestException) t).getHttpStatusCode();\r
-                                       if (statusCode == 405)\r
-                                               app.displayError("You don't have the necessary permissions");\r
-                                       else if (statusCode == 404)\r
-                                               app.displayError("User in permissions does not exist");\r
-                                       else if (statusCode == 409)\r
-                                               app.displayError("A folder with the same name already exists");\r
-                                       else if (statusCode == 413)\r
-                                               app.displayError("Your quota has been exceeded");\r
-                                       else\r
-                                               app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
-                               } else\r
-                                       app.displayError("System error moifying file:" + t.getMessage());\r
-                       }\r
-               };\r
-               DeferredCommand.addCommand(cf);\r
-       }\r
-\r
-       private void toggleVersioned(boolean versionedValue) {\r
-               JSONObject json = new JSONObject();\r
-               json.put("versioned", JSONBoolean.getInstance(versionedValue));\r
-               GWT.log(json.toString(), null);\r
-               PostCommand cf = new PostCommand(app, file.getUri() + "?update=", json.toString(), 200) {\r
-\r
-                       @Override\r
-                       public void onComplete() {\r
-                               app.getTreeView().refreshCurrentNode(false);\r
-                       }\r
-\r
-                       @Override\r
-                       public void onError(Throwable t) {\r
-                               GWT.log("", t);\r
-                               if (t instanceof RestException) {\r
-                                       int statusCode = ((RestException) t).getHttpStatusCode();\r
-                                       if (statusCode == 405)\r
-                                               app.displayError("You don't have the necessary permissions");\r
-                                       else if (statusCode == 404)\r
-                                               app.displayError("User in permissions does not exist");\r
-                                       else if (statusCode == 409)\r
-                                               app.displayError("A folder with the same name already exists");\r
-                                       else if (statusCode == 413)\r
-                                               app.displayError("Your quota has been exceeded");\r
-                                       else\r
-                                               app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
-                               } else\r
-                                       app.displayError("System error moifying file:" + t.getMessage());\r
-                       }\r
-               };\r
-               DeferredCommand.addCommand(cf);\r
        }\r
-\r
 }\r