Removed commented code
[pithos-web-client] / src / gr / grnet / pithos / web / client / FilePermissionsDialog.java
1 /*\r
2  * Copyright 2011 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.foldertree.File;\r
38 import gr.grnet.pithos.web.client.foldertree.Resource;\r
39 import gr.grnet.pithos.web.client.rest.PostRequest;\r
40 \r
41 import java.util.Map;\r
42 \r
43 import com.google.gwt.core.client.GWT;\r
44 import com.google.gwt.core.client.Scheduler;\r
45 import com.google.gwt.event.dom.client.ClickEvent;\r
46 import com.google.gwt.event.dom.client.ClickHandler;\r
47 import com.google.gwt.http.client.Response;\r
48 import com.google.gwt.http.client.URL;\r
49 import com.google.gwt.resources.client.ImageResource;\r
50 import com.google.gwt.user.client.Command;\r
51 import com.google.gwt.user.client.Window;\r
52 import com.google.gwt.user.client.ui.Anchor;\r
53 import com.google.gwt.user.client.ui.Button;\r
54 import com.google.gwt.user.client.ui.CheckBox;\r
55 import com.google.gwt.user.client.ui.DecoratedTabPanel;\r
56 import com.google.gwt.user.client.ui.FocusPanel;\r
57 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
58 import com.google.gwt.user.client.ui.HorizontalPanel;\r
59 import com.google.gwt.user.client.ui.Label;\r
60 import com.google.gwt.user.client.ui.TextBox;\r
61 import com.google.gwt.user.client.ui.VerticalPanel;\r
62 \r
63 /**\r
64  * The 'File properties' dialog box implementation.\r
65  *\r
66  */\r
67 public class FilePermissionsDialog extends AbstractPropertiesDialog {\r
68 \r
69         protected PermissionsList permList;\r
70 \r
71         protected CheckBox readForAll;\r
72 \r
73         /**\r
74          * An image bundle for this widgets images.\r
75          */\r
76         public interface Images extends MessagePanel.Images {\r
77 \r
78                 @Source("gr/grnet/pithos/resources/edit_user.png")\r
79                 ImageResource permUser();\r
80 \r
81                 @Source("gr/grnet/pithos/resources/groups22.png")\r
82                 ImageResource permGroup();\r
83 \r
84                 @Source("gr/grnet/pithos/resources/editdelete.png")\r
85                 ImageResource delete();\r
86         }\r
87 \r
88         final File file;\r
89 \r
90     Images images = GWT.create(Images.class);\r
91 \r
92         /**\r
93          * The widget's constructor.\r
94          */\r
95         public FilePermissionsDialog(Pithos _app, File _file) {\r
96         super(_app);\r
97         file = _file;\r
98 \r
99                 Anchor close = new Anchor();\r
100                 close.addStyleName("close");\r
101                 close.addClickHandler(new ClickHandler() {\r
102                         \r
103                         @Override\r
104                         public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
105                                 hide();\r
106                         }\r
107                 });\r
108                 // Set the dialog's caption.\r
109                 setText("File permissions");\r
110                 setAnimationEnabled(true);\r
111                 setGlassEnabled(true);\r
112                 setStyleName("pithos-DialogBox");\r
113 \r
114                 // Outer contains inner and buttons.\r
115                 final VerticalPanel outer = new VerticalPanel();\r
116                 outer.add(close);\r
117                 final FocusPanel focusPanel = new FocusPanel(outer);\r
118                 // Inner contains generalPanel and permPanel.\r
119                 inner = new VerticalPanel();\r
120                 inner.addStyleName("inner");\r
121 \r
122         inner.add(createSharingPanel());\r
123 \r
124         outer.add(inner);\r
125 \r
126                 // Create the 'OK' button, along with a listener that hides the dialog\r
127                 // when the button is clicked.\r
128                 final Button ok = new Button("OK", new ClickHandler() {\r
129                         @Override\r
130                         public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
131                                 accept();\r
132                                 closeDialog();\r
133                         }\r
134                 });\r
135                 ok.addStyleName("button");\r
136 \r
137         outer.add(ok);\r
138         outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);\r
139 \r
140         focusPanel.setFocus(true);\r
141         setWidget(outer);\r
142         }\r
143 \r
144     private VerticalPanel createSharingPanel() {\r
145         VerticalPanel permPanel = new VerticalPanel();\r
146 \r
147         permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);\r
148         permPanel.add(permList);\r
149 \r
150         if (file.getInheritedPermissionsFrom() == null) {\r
151             HorizontalPanel permButtons = new HorizontalPanel();\r
152             Button add = new Button("Add Group", new ClickHandler() {\r
153                 @Override\r
154                 public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
155                     PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);\r
156                     dlg.center();\r
157                     permList.updatePermissionTable();\r
158                 }\r
159             });\r
160             add.addStyleName("button");\r
161             permButtons.add(add);\r
162             permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);\r
163 \r
164             final Button addUser = new Button("Add User", new ClickHandler() {\r
165                 @Override\r
166                 public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
167                     PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);\r
168                     dlg.center();\r
169                     permList.updatePermissionTable();\r
170                 }\r
171             });\r
172             addUser.addStyleName("button");\r
173             permButtons.add(addUser);\r
174             permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);\r
175 \r
176             permButtons.setSpacing(8);\r
177             permButtons.addStyleName("pithos-TabPanelBottom");\r
178             permPanel.add(permButtons);\r
179         }\r
180 \r
181         final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +\r
182                     " by everyone. By checking this option, you are certifying that you have the right to " +\r
183                     "distribute this file and that it does not violate the Terms of Use.", true);\r
184         readForAllNote.setVisible(false);\r
185         readForAllNote.setStylePrimaryName("pithos-readForAllNote");\r
186 \r
187         readForAll = new CheckBox();\r
188         readForAll.setValue(file.isPublished());\r
189         readForAll.addClickHandler(new ClickHandler() {\r
190             @Override\r
191             public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
192                 readForAllNote.setVisible(readForAll.getValue());\r
193             }\r
194         });\r
195 \r
196         // Only show the read for all permission if the user is the owner.\r
197         if (file.getOwner().equals(app.getUsername())) {\r
198             final HorizontalPanel permForAll = new HorizontalPanel();\r
199             permForAll.add(new Label("Public"));\r
200             permForAll.add(readForAll);\r
201             permForAll.setSpacing(8);\r
202             permForAll.addStyleName("pithos-TabPanelBottom");\r
203             permForAll.add(readForAllNote);\r
204             permPanel.add(permForAll);\r
205         }\r
206 \r
207         if (file.isPublished()) {\r
208             final HorizontalPanel pathPanel = new HorizontalPanel();\r
209             pathPanel.setWidth("100%");\r
210             pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);\r
211             pathPanel.add(new Label("Link"));\r
212             pathPanel.setSpacing(8);\r
213             pathPanel.addStyleName("pithos-TabPanelBottom");\r
214 \r
215             TextBox path = new TextBox();\r
216             path.setWidth("100%");\r
217             path.addClickHandler(new ClickHandler() {\r
218                 @Override\r
219                 public void onClick(ClickEvent event) {\r
220                     Pithos.enableIESelection();\r
221                     ((TextBox) event.getSource()).selectAll();\r
222                     Pithos.preventIESelection();\r
223                 }\r
224             });\r
225             path.setText(Window.Location.getHost() + file.getPublicUri());\r
226             path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");\r
227             path.setWidth("100%");\r
228             path.setReadOnly(true);\r
229             pathPanel.add(path);\r
230             permPanel.add(pathPanel);\r
231         }\r
232 \r
233         return permPanel;\r
234     }\r
235 \r
236         /**\r
237          * Accepts any change and updates the file\r
238          *\r
239          */\r
240         @Override\r
241         protected void accept() {\r
242                 final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);\r
243 \r
244                 //only update the read for all perm if the user is the owner\r
245         Boolean published = null;\r
246                 if (readForAll.getValue() != file.isPublished())\r
247                         if (file.getOwner().equals(app.getUsername()))\r
248                 published = readForAll.getValue();\r
249         final Boolean finalPublished = published;\r
250 \r
251         updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", finalPublished, perms);\r
252         }\r
253 \r
254         protected void updateMetaData(String api, String owner, String path, final Boolean published, final Map<String, Boolean[]> newPermissions) {\r
255         if (published != null || newPermissions != null) {\r
256             PostRequest updateFile = new PostRequest(api, owner, path) {\r
257                 @Override\r
258                 public void onSuccess(@SuppressWarnings("unused") Resource result) {\r
259                     app.updateFolder(file.getParent(), true, new Command() {\r
260                                                 \r
261                                                 @Override\r
262                                                 public void execute() {\r
263                                                         app.updateMySharedRoot();\r
264                                                 }\r
265                                         });\r
266                 }\r
267 \r
268                 @Override\r
269                 public void onError(Throwable t) {\r
270                     GWT.log("", t);\r
271                                         app.setError(t);\r
272                     app.displayError("System error modifying file:" + t.getMessage());\r
273                 }\r
274 \r
275                                 @Override\r
276                                 protected void onUnauthorized(@SuppressWarnings("unused") Response response) {\r
277                                         app.sessionExpired();\r
278                                 }\r
279             };\r
280             updateFile.setHeader("X-Auth-Token", app.getToken());\r
281             \r
282             if (published != null)\r
283                 updateFile.setHeader("X-Object-Public", published.toString());\r
284             if (newPermissions != null) {\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                 updateFile.setHeader("X-Object-Sharing", permHeader);\r
308             }\r
309             Scheduler.get().scheduleDeferred(updateFile);\r
310         }\r
311         else\r
312             app.updateFolder(file.getParent(), true, new Command() {\r
313                                 \r
314                                 @Override\r
315                                 public void execute() {\r
316                                         if (file.isShared())\r
317                                                 app.updateMySharedRoot();\r
318                                 }\r
319                         });\r
320     }\r
321 }\r