New button "Add everybody" to file Share dialog
[pithos-web-client] / src / gr / grnet / pithos / web / client / PermissionsAddDialog.java
index c2fa97c..6ebca4f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2013 GRNET S.A. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or
  * without modification, are permitted provided that the following
@@ -34,6 +34,8 @@
  */
 package gr.grnet.pithos.web.client;
 
+import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
+import gr.grnet.pithos.web.client.catalog.UserCatalogs;
 import gr.grnet.pithos.web.client.grouptree.Group;
 
 import java.util.List;
@@ -42,26 +44,28 @@ 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.regexp.shared.RegExp;
 import com.google.gwt.user.client.Event.NativePreviewEvent;
 import com.google.gwt.user.client.ui.Anchor;
 import com.google.gwt.user.client.ui.Button;
-import com.google.gwt.user.client.ui.CheckBox;
 import com.google.gwt.user.client.ui.DialogBox;
 import com.google.gwt.user.client.ui.FlexTable;
 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 import com.google.gwt.user.client.ui.ListBox;
+import com.google.gwt.user.client.ui.RadioButton;
 import com.google.gwt.user.client.ui.TextBox;
 import com.google.gwt.user.client.ui.VerticalPanel;
 
 public class PermissionsAddDialog extends DialogBox {
+    final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
 
        private TextBox userBox = new TextBox();
 
        private ListBox groupBox = new ListBox();
 
-       private CheckBox read = new CheckBox();
+       private RadioButton read = new RadioButton("permissions");
 
-       private CheckBox write = new CheckBox();
+       private RadioButton write = new RadioButton("permissions");
 
        private PermissionsList permList;
 
@@ -74,7 +78,7 @@ public class PermissionsAddDialog extends DialogBox {
                userAdd = _userAdd;
                permList = _permList;
 
-               Anchor close = new Anchor();
+               Anchor close = new Anchor("close");
                close.addStyleName("close");
                close.addClickHandler(new ClickHandler() {
                        
@@ -94,8 +98,8 @@ public class PermissionsAddDialog extends DialogBox {
 
         final FlexTable permTable = new FlexTable();
         permTable.setText(0, 0, "Users/Groups");
-        permTable.setText(0, 1, "Read");
-        permTable.setText(0, 2, "Write");
+        permTable.setText(0, 1, "Read Only");
+        permTable.setText(0, 2, "Read/Write");
         permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
         permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
         permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
@@ -104,11 +108,13 @@ public class PermissionsAddDialog extends DialogBox {
             permTable.setWidget(1, 0, userBox);
         }
         else {
-            for (Group group : _groups)
+            for (Group group : _groups) {
                 groupBox.addItem(group.getName(), group.getName());
+            }
             permTable.setWidget(1, 0, groupBox);
         }
-
+                
+        read.setValue(true);
         permTable.setWidget(1, 1, read);
         permTable.setWidget(1, 2, write);
 
@@ -135,22 +141,65 @@ public class PermissionsAddDialog extends DialogBox {
        }
 
        protected void addPermission() {
+        final boolean readValue = read.getValue();
+        final boolean writeValue = write.getValue();
+
         String selected = null;
                if (userAdd) {
-                       selected = userBox.getText();
-               } else {
+                       final String userDisplayName = userBox.getText().trim();
+                       addUserPermission(userDisplayName, readValue, writeValue);
+            return;
+               } else if (groupBox.getSelectedIndex() > -1) {
                        String groupName = groupBox.getValue(groupBox.getSelectedIndex());
-            selected = app.getUsername() + ":" + groupName;
+                       selected = app.getUserID() + ":" + groupName;
                }
         if (permList.getPermissions().get(selected) != null) {
             return;
         }
-               boolean readValue = read.getValue();
-               boolean writeValue = write.getValue();
+        if (selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) {
+               app.displayWarning("You have to select a username or group");
+               return;
+        }
 
                permList.addPermission(selected, readValue, writeValue);
        }
 
+    private boolean alreadyHasPermission(String selected) {
+        return permList.getPermissions().get(selected) != null;
+    }
+
+    private void addUserPermission(final String userDisplayName, final boolean readValue, final boolean writeValue) {
+        if (!EmailValidator.test(userDisplayName)) {
+            app.displayWarning("Username must be a valid email address");
+            return;
+        }
+
+        // Now get the userID
+        final String userID = app.getIDForUserDisplayName(userDisplayName);
+        if(userID != null) {
+            // Check if already have the permission
+            if(!alreadyHasPermission(userID)) {
+                permList.addPermission(userID, readValue, writeValue);
+            }
+        }
+        else {
+            // Must call server to obtain userID
+            new UpdateUserCatalogs(app, null, Helpers.toList(userDisplayName)) {
+                @Override
+                public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
+                    final String userID = updatedUserCatalogs.getID(userDisplayName);
+                    if(userID == null) {
+                        Pithos.LOG("PermissionsDialog::addUserPermission(), UpdateUserCatalogs() => Unknown user ", userDisplayName);
+                        app.displayWarning("Unknown user " + userDisplayName);
+                    }
+                    else if(!alreadyHasPermission(userID)) {
+                        permList.addPermission(userID, readValue, writeValue);
+                    }
+                }
+            }.scheduleDeferred();
+        }
+    }
+
        @Override
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
                super.onPreviewNativeEvent(preview);