Removed animation that is broken by latest chrome (v.24)
[pithos-web-client] / src / gr / grnet / pithos / web / client / FolderPermissionsDialog.java
1 /*\r
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.\r
3  *\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
7  *\r
8  *   1. Redistributions of source code must retain the above\r
9  *      copyright notice, this list of conditions and the following\r
10  *      disclaimer.\r
11  *\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
16  *\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
29  *\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
34  */\r
35 package gr.grnet.pithos.web.client;\r
36 \r
37 import gr.grnet.pithos.web.client.commands.CreateGroupCommand;\r
38 import gr.grnet.pithos.web.client.foldertree.File;\r
39 import gr.grnet.pithos.web.client.foldertree.Folder;\r
40 import gr.grnet.pithos.web.client.foldertree.Resource;\r
41 import gr.grnet.pithos.web.client.rest.PostRequest;\r
42 import gr.grnet.pithos.web.client.rest.PutRequest;\r
43 import gr.grnet.pithos.web.client.rest.RestException;\r
44 \r
45 import java.util.Iterator;\r
46 import java.util.Map;\r
47 \r
48 import com.google.gwt.core.client.GWT;\r
49 import com.google.gwt.core.client.Scheduler;\r
50 import com.google.gwt.dom.client.NativeEvent;\r
51 import com.google.gwt.event.dom.client.ClickEvent;\r
52 import com.google.gwt.event.dom.client.ClickHandler;\r
53 import com.google.gwt.event.dom.client.KeyCodes;\r
54 import com.google.gwt.event.dom.client.KeyDownEvent;\r
55 import com.google.gwt.http.client.Response;\r
56 import com.google.gwt.http.client.URL;\r
57 import com.google.gwt.user.client.Command;\r
58 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
59 import com.google.gwt.user.client.ui.Anchor;\r
60 import com.google.gwt.user.client.ui.Button;\r
61 import com.google.gwt.user.client.ui.DialogBox;\r
62 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
63 import com.google.gwt.user.client.ui.HorizontalPanel;\r
64 import com.google.gwt.user.client.ui.Label;\r
65 import com.google.gwt.user.client.ui.VerticalPanel;\r
66 \r
67 /**\r
68  * The 'Folder properties' dialog box implementation.\r
69  */\r
70 public class FolderPermissionsDialog extends DialogBox {\r
71 \r
72     protected Pithos app;\r
73 \r
74         /**\r
75          * The widget that holds the folderName of the folder.\r
76          */\r
77         Label folderName = new Label();\r
78 \r
79         protected PermissionsList permList;\r
80 \r
81         final Folder folder;\r
82 \r
83         final VerticalPanel inner;\r
84 \r
85         /**\r
86          * The widget's constructor.\r
87          */\r
88         public FolderPermissionsDialog(final Pithos app, Folder selected) {\r
89         this.app = app;\r
90                 Anchor close = new Anchor("close");\r
91                 close.addStyleName("close");\r
92                 close.addClickHandler(new ClickHandler() {\r
93                         \r
94                         @Override\r
95                         public void onClick(ClickEvent event) {\r
96                                 hide();\r
97                         }\r
98                 });\r
99 \r
100                 setGlassEnabled(true);\r
101                 setStyleName("pithos-DialogBox");\r
102 \r
103                 // Enable IE selection for the dialog (must disable it upon closing it)\r
104                 Pithos.enableIESelection();\r
105 \r
106                 folder = selected;\r
107 \r
108                 setText("Folder permissions");\r
109 \r
110                 // Outer contains inner and buttons\r
111                 VerticalPanel outer = new VerticalPanel();\r
112                 outer.add(close);\r
113                 // Inner contains generalPanel and permPanel\r
114                 inner = new VerticalPanel();\r
115                 inner.addStyleName("inner");\r
116 \r
117 \r
118                 folderName.setText(folder.getName());\r
119 \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
124 \r
125         HorizontalPanel permButtons = new HorizontalPanel();\r
126         Button addUser = new Button("Add User", new ClickHandler() {\r
127             @Override\r
128             public void onClick(ClickEvent event) {\r
129                 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);\r
130                 dlg.center();\r
131             }\r
132         });\r
133         addUser.addStyleName("button");\r
134         permButtons.add(addUser);\r
135         permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);\r
136 \r
137         Button add = new Button("Add Group", new ClickHandler() {\r
138             @Override\r
139             public void onClick(ClickEvent event) {\r
140                 if (app.getAccount().getGroups().isEmpty()) {\r
141                     new GroupCreateDialog(app, new Command() {\r
142                                                 \r
143                                                 @Override\r
144                                                 public void execute() {\r
145                                         if (app.getAccount().getGroups().isEmpty())\r
146                                                 return;\r
147                                         PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);\r
148                                         dlg.center();\r
149                                                 }\r
150                                         }).center();\r
151                 }\r
152                 else {\r
153                         PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);\r
154                         dlg.center();\r
155                 }\r
156             }\r
157         });\r
158         add.addStyleName("button");\r
159         permButtons.add(add);\r
160         permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);\r
161 \r
162         permButtons.setSpacing(8);\r
163         permPanel.add(permButtons);\r
164 \r
165         inner.add(permPanel);\r
166 \r
167         outer.add(inner);\r
168 \r
169                 // Create the 'Create/Update' button, along with a listener that hides the dialog\r
170                 // when the button is clicked and quits the application.\r
171                 String okLabel = "Update";\r
172                 final Button ok = new Button(okLabel, new ClickHandler() {\r
173                         @Override\r
174                         public void onClick(ClickEvent event) {\r
175                                 updateFolder();\r
176                                 closeDialog();\r
177                         }\r
178                 });\r
179                 ok.addStyleName("button");\r
180                 outer.add(ok);\r
181         outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);\r
182 \r
183         setWidget(outer);\r
184         }\r
185 \r
186         @Override\r
187         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
188                 super.onPreviewNativeEvent(preview);\r
189 \r
190                 NativeEvent evt = preview.getNativeEvent();\r
191                 if (evt.getType().equals(KeyDownEvent.getType().getName()))\r
192                         // Use the popup's key preview hooks to close the dialog when either\r
193                         // enter or escape is pressed.\r
194                         switch (evt.getKeyCode()) {\r
195                                 case KeyCodes.KEY_ENTER:\r
196                                         updateFolder();\r
197                     closeDialog();\r
198                                         break;\r
199                                 case KeyCodes.KEY_ESCAPE:\r
200                                         closeDialog();\r
201                                         break;\r
202                         }\r
203         }\r
204 \r
205 \r
206         /**\r
207          * Enables IE selection prevention and hides the dialog\r
208          * (we disable the prevention on creation of the dialog)\r
209          */\r
210         public void closeDialog() {\r
211                 Pithos.preventIESelection();\r
212                 hide();\r
213         }\r
214 \r
215         void updateFolder() {\r
216         final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);\r
217         updateMetadata(folder.getUri() + "?update=", perms);\r
218         }\r
219 \r
220         protected void updateMetadata(final String path, final Map<String, Boolean[]> newPermissions) {\r
221         if (newPermissions != null) {\r
222             PostRequest updateFolder = new PostRequest(app.getApiPath(), folder.getOwner(), path) {\r
223                 @Override\r
224                 public void onSuccess(Resource result) {\r
225                     app.updateFolder(folder.getParent(), false, new Command() {\r
226                                                 \r
227                                                 @Override\r
228                                                 public void execute() {\r
229                                                         app.updateMySharedRoot();\r
230                                                 }\r
231                                         }, true);\r
232                 }\r
233 \r
234                 @Override\r
235                 public void onError(Throwable t) {\r
236                     GWT.log("", t);\r
237                                         app.setError(t);\r
238                     if (t instanceof RestException) {\r
239                         if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) { //Probably a virtual folder\r
240                             final String path1 = folder.getUri();\r
241                             PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path1) {\r
242                                 @Override\r
243                                 public void onSuccess(Resource result) {\r
244                                         updateMetadata(path, newPermissions);\r
245                                 }\r
246 \r
247                                 @Override\r
248                                 public void onError(Throwable _t) {\r
249                                     GWT.log("", _t);\r
250                                                         app.setError(_t);\r
251                                     if(_t instanceof RestException){\r
252                                         app.displayError("Unable to update folder: " + ((RestException) _t).getHttpStatusText());\r
253                                     }\r
254                                     else\r
255                                         app.displayError("System error modifying folder: " + _t.getMessage());\r
256                                 }\r
257 \r
258                                                 @Override\r
259                                                 protected void onUnauthorized(Response response) {\r
260                                                         app.sessionExpired();\r
261                                                 }\r
262                             };\r
263                             newFolder.setHeader("X-Auth-Token", app.getToken());\r
264                             newFolder.setHeader("Content-Type", "application/folder");\r
265                             newFolder.setHeader("Accept", "*/*");\r
266                             newFolder.setHeader("Content-Length", "0");\r
267                             Scheduler.get().scheduleDeferred(newFolder);\r
268                         }\r
269                         else if (((RestException) t).getHttpStatusCode() == Response.SC_CONFLICT) {\r
270                                 app.displayError("Cannot set permissions. Probably subfolders or files already have permissions set");\r
271                         }\r
272                         else\r
273                                 app.displayError("Εrror modifying folder: " + t.getMessage());\r
274                     }\r
275                     else\r
276                         app.displayError("System error modifying folder: " + t.getMessage());\r
277                 }\r
278 \r
279                                 @Override\r
280                                 protected void onUnauthorized(Response response) {\r
281                                         app.sessionExpired();\r
282                                 }\r
283             };\r
284             updateFolder.setHeader("X-Auth-Token", app.getToken());\r
285             String readPermHeader = "read=";\r
286             String writePermHeader = "write=";\r
287             for (String u : newPermissions.keySet()) {\r
288                 Boolean[] p = newPermissions.get(u);\r
289                 if (p[0] != null && p[0])\r
290                     readPermHeader += u + ",";\r
291                 if (p[1] != null && p[1])\r
292                     writePermHeader += u + ",";\r
293             }\r
294             if (readPermHeader.endsWith("="))\r
295                 readPermHeader = "";\r
296             else if (readPermHeader.endsWith(","))\r
297                 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);\r
298             if (writePermHeader.endsWith("="))\r
299                 writePermHeader = "";\r
300             else if (writePermHeader.endsWith(","))\r
301                 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);\r
302             String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;\r
303             if (permHeader.length() == 0)\r
304                 permHeader = "~";\r
305             else\r
306                 permHeader = URL.encodePathSegment(permHeader);\r
307             updateFolder.setHeader("X-Object-Sharing", permHeader);\r
308             Scheduler.get().scheduleDeferred(updateFolder);\r
309         }\r
310         else\r
311             app.updateFolder(folder.getParent(), false, new Command() {\r
312                                 \r
313                                 @Override\r
314                                 public void execute() {\r
315                                         app.updateMySharedRoot();\r
316                                 }\r
317                         }, true);\r
318     }\r
319 }\r