2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
\r
4 * Redistribution and use in source and binary forms, with or
\r
5 * without modification, are permitted provided that the following
\r
6 * conditions are met:
\r
8 * 1. Redistributions of source code must retain the above
\r
9 * copyright notice, this list of conditions and the following
\r
12 * 2. Redistributions in binary form must reproduce the above
\r
13 * copyright notice, this list of conditions and the following
\r
14 * disclaimer in the documentation and/or other materials
\r
15 * provided with the distribution.
\r
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
\r
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
\r
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
\r
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
\r
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
28 * POSSIBILITY OF SUCH DAMAGE.
\r
30 * The views and conclusions contained in the software and
\r
31 * documentation are those of the authors and should not be
\r
32 * interpreted as representing official policies, either expressed
\r
33 * or implied, of GRNET S.A.
\r
35 package gr.grnet.pithos.web.client;
\r
37 import gr.grnet.pithos.web.client.foldertree.File;
\r
38 import gr.grnet.pithos.web.client.foldertree.Folder;
\r
39 import gr.grnet.pithos.web.client.foldertree.Resource;
\r
40 import gr.grnet.pithos.web.client.rest.PostRequest;
\r
41 import gr.grnet.pithos.web.client.rest.PutRequest;
\r
42 import gr.grnet.pithos.web.client.rest.RestException;
\r
44 import java.util.Iterator;
\r
45 import java.util.Map;
\r
47 import com.google.gwt.core.client.GWT;
\r
48 import com.google.gwt.core.client.Scheduler;
\r
49 import com.google.gwt.dom.client.NativeEvent;
\r
50 import com.google.gwt.event.dom.client.ClickEvent;
\r
51 import com.google.gwt.event.dom.client.ClickHandler;
\r
52 import com.google.gwt.event.dom.client.KeyCodes;
\r
53 import com.google.gwt.event.dom.client.KeyDownEvent;
\r
54 import com.google.gwt.http.client.Response;
\r
55 import com.google.gwt.http.client.URL;
\r
56 import com.google.gwt.user.client.Command;
\r
57 import com.google.gwt.user.client.Event.NativePreviewEvent;
\r
58 import com.google.gwt.user.client.ui.Anchor;
\r
59 import com.google.gwt.user.client.ui.Button;
\r
60 import com.google.gwt.user.client.ui.DialogBox;
\r
61 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
62 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
63 import com.google.gwt.user.client.ui.Label;
\r
64 import com.google.gwt.user.client.ui.VerticalPanel;
\r
67 * The 'Folder properties' dialog box implementation.
\r
69 public class FolderPermissionsDialog extends DialogBox {
\r
71 protected Pithos app;
\r
74 * The widget that holds the folderName of the folder.
\r
76 Label folderName = new Label();
\r
78 protected PermissionsList permList;
\r
80 final Folder folder;
\r
82 final VerticalPanel inner;
\r
85 * The widget's constructor.
\r
87 public FolderPermissionsDialog(final Pithos app, Folder selected) {
\r
89 Anchor close = new Anchor();
\r
90 close.addStyleName("close");
\r
91 close.addClickHandler(new ClickHandler() {
\r
94 public void onClick(ClickEvent event) {
\r
99 setAnimationEnabled(true);
\r
100 setGlassEnabled(true);
\r
101 setStyleName("pithos-DialogBox");
\r
103 // Enable IE selection for the dialog (must disable it upon closing it)
\r
104 Pithos.enableIESelection();
\r
108 setText("Folder permissions");
\r
110 // Outer contains inner and buttons
\r
111 VerticalPanel outer = new VerticalPanel();
\r
113 // Inner contains generalPanel and permPanel
\r
114 inner = new VerticalPanel();
\r
115 inner.addStyleName("inner");
\r
118 folderName.setText(folder.getName());
\r
120 VerticalPanel permPanel = new VerticalPanel();
\r
121 FilePermissionsDialog.Images images = GWT.create(FilePermissionsDialog.Images.class);
\r
122 permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner(), false, null);
\r
123 permPanel.add(permList);
\r
125 HorizontalPanel permButtons = new HorizontalPanel();
\r
126 Button addUser = new Button("Add User", new ClickHandler() {
\r
128 public void onClick(ClickEvent event) {
\r
129 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
\r
133 addUser.addStyleName("button");
\r
134 permButtons.add(addUser);
\r
135 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
137 Button add = new Button("Add Group", new ClickHandler() {
\r
139 public void onClick(ClickEvent event) {
\r
140 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
\r
144 add.addStyleName("button");
\r
145 permButtons.add(add);
\r
146 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
148 permButtons.setSpacing(8);
\r
149 permPanel.add(permButtons);
\r
151 inner.add(permPanel);
\r
155 // Create the 'Create/Update' button, along with a listener that hides the dialog
\r
156 // when the button is clicked and quits the application.
\r
157 String okLabel = "Update";
\r
158 final Button ok = new Button(okLabel, new ClickHandler() {
\r
160 public void onClick(ClickEvent event) {
\r
165 ok.addStyleName("button");
\r
167 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
\r
173 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
174 super.onPreviewNativeEvent(preview);
\r
176 NativeEvent evt = preview.getNativeEvent();
\r
177 if (evt.getType().equals(KeyDownEvent.getType().getName()))
\r
178 // Use the popup's key preview hooks to close the dialog when either
\r
179 // enter or escape is pressed.
\r
180 switch (evt.getKeyCode()) {
\r
181 case KeyCodes.KEY_ENTER:
\r
185 case KeyCodes.KEY_ESCAPE:
\r
193 * Enables IE selection prevention and hides the dialog
\r
194 * (we disable the prevention on creation of the dialog)
\r
196 public void closeDialog() {
\r
197 Pithos.preventIESelection();
\r
201 void updateFolder() {
\r
202 final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
\r
203 final String newName = folderName.getText().trim();
\r
204 if (!folder.isContainer() && !folder.getName().equals(newName)) {
\r
205 final String path = folder.getParent().getUri() + "/" + newName;
\r
206 PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getParent().getOwner(), path) {
\r
208 public void onSuccess(Resource result) {
\r
209 Iterator<File> iter = folder.getFiles().iterator();
\r
210 app.copyFiles(iter, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
\r
212 public void execute() {
\r
213 Iterator<Folder> iterf = folder.getSubfolders().iterator();
\r
214 app.copySubfolders(iterf, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
\r
216 public void execute() {
\r
217 app.deleteFolder(folder, null);
\r
218 updateMetadata(path + "?update=", perms);
\r
226 public void onError(Throwable t) {
\r
229 if(t instanceof RestException){
\r
230 app.displayError("Unable to update folder: " + ((RestException) t).getHttpStatusText());
\r
233 app.displayError("System error modifying folder: " + t.getMessage());
\r
237 protected void onUnauthorized(Response response) {
\r
238 app.sessionExpired();
\r
241 newFolder.setHeader("X-Auth-Token", app.getToken());
\r
242 newFolder.setHeader("Content-Type", "application/folder");
\r
243 newFolder.setHeader("Accept", "*/*");
\r
244 newFolder.setHeader("Content-Length", "0");
\r
245 Scheduler.get().scheduleDeferred(newFolder);
\r
248 updateMetadata(folder.getUri() + "?update=", perms);
\r
251 protected void updateMetadata(final String path, final Map<String, Boolean[]> newPermissions) {
\r
252 if (newPermissions != null) {
\r
253 PostRequest updateFolder = new PostRequest(app.getApiPath(), folder.getOwner(), path) {
\r
255 public void onSuccess(Resource result) {
\r
256 app.updateFolder(folder.getParent(), false, new Command() {
\r
259 public void execute() {
\r
260 app.updateMySharedRoot();
\r
266 public void onError(Throwable t) {
\r
269 if (t instanceof RestException) {
\r
270 if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) { //Probably a virtual folder
\r
271 final String path1 = folder.getUri();
\r
272 PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path1) {
\r
274 public void onSuccess(Resource result) {
\r
275 updateMetadata(path, newPermissions);
\r
279 public void onError(Throwable _t) {
\r
282 if(_t instanceof RestException){
\r
283 app.displayError("Unable to update folder: " + ((RestException) _t).getHttpStatusText());
\r
286 app.displayError("System error modifying folder: " + _t.getMessage());
\r
290 protected void onUnauthorized(Response response) {
\r
291 app.sessionExpired();
\r
294 newFolder.setHeader("X-Auth-Token", app.getToken());
\r
295 newFolder.setHeader("Content-Type", "application/folder");
\r
296 newFolder.setHeader("Accept", "*/*");
\r
297 newFolder.setHeader("Content-Length", "0");
\r
298 Scheduler.get().scheduleDeferred(newFolder);
\r
300 else if (((RestException) t).getHttpStatusCode() == Response.SC_CONFLICT) {
\r
301 app.displayError("Cannot set permissions. Probably subfolders or files already have permissions set");
\r
304 app.displayError("Εrror modifying folder: " + t.getMessage());
\r
307 app.displayError("System error modifying folder: " + t.getMessage());
\r
311 protected void onUnauthorized(Response response) {
\r
312 app.sessionExpired();
\r
315 updateFolder.setHeader("X-Auth-Token", app.getToken());
\r
316 String readPermHeader = "read=";
\r
317 String writePermHeader = "write=";
\r
318 for (String u : newPermissions.keySet()) {
\r
319 Boolean[] p = newPermissions.get(u);
\r
320 if (p[0] != null && p[0])
\r
321 readPermHeader += u + ",";
\r
322 if (p[1] != null && p[1])
\r
323 writePermHeader += u + ",";
\r
325 if (readPermHeader.endsWith("="))
\r
326 readPermHeader = "";
\r
327 else if (readPermHeader.endsWith(","))
\r
328 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
\r
329 if (writePermHeader.endsWith("="))
\r
330 writePermHeader = "";
\r
331 else if (writePermHeader.endsWith(","))
\r
332 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
\r
333 String permHeader = readPermHeader + ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? ";" : "") + writePermHeader;
\r
334 if (permHeader.length() == 0)
\r
337 permHeader = URL.encodePathSegment(permHeader);
\r
338 updateFolder.setHeader("X-Object-Sharing", permHeader);
\r
339 Scheduler.get().scheduleDeferred(updateFolder);
\r
342 app.updateFolder(folder.getParent(), false, new Command() {
\r
345 public void execute() {
\r
346 app.updateMySharedRoot();
\r