All close buttons renamed to OK buttons in dialogs (issue #2374)
[pithos-web-client] / src / gr / grnet / pithos / web / client / FilePermissionsDialog.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.foldertree.File;\r
38 import gr.grnet.pithos.web.client.foldertree.Resource;\r
39 import gr.grnet.pithos.web.client.rest.HeadRequest;\r
40 import gr.grnet.pithos.web.client.rest.PostRequest;\r
41 \r
42 import java.util.Map;\r
43 \r
44 import com.google.gwt.core.client.GWT;\r
45 import com.google.gwt.core.client.Scheduler;\r
46 import com.google.gwt.dom.client.NativeEvent;\r
47 import com.google.gwt.event.dom.client.ClickEvent;\r
48 import com.google.gwt.event.dom.client.ClickHandler;\r
49 import com.google.gwt.event.dom.client.KeyCodes;\r
50 import com.google.gwt.http.client.Response;\r
51 import com.google.gwt.http.client.URL;\r
52 import com.google.gwt.http.client.UrlBuilder;\r
53 import com.google.gwt.i18n.client.Dictionary;\r
54 import com.google.gwt.resources.client.ImageResource;\r
55 import com.google.gwt.user.client.Command;\r
56 import com.google.gwt.user.client.Window;\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.CheckBox;\r
61 import com.google.gwt.user.client.ui.FocusPanel;\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.TextBox;\r
66 import com.google.gwt.user.client.ui.VerticalPanel;\r
67 \r
68 /**\r
69  * The 'File properties' dialog box implementation.\r
70  *\r
71  */\r
72 public class FilePermissionsDialog extends AbstractPropertiesDialog {\r
73 \r
74         protected PermissionsList permList;\r
75 \r
76         private HorizontalPanel pathPanel;\r
77         \r
78         private TextBox path;\r
79         \r
80         /**\r
81          * An image bundle for this widgets images.\r
82          */\r
83         public interface Images extends MessagePanel.Images {\r
84 \r
85                 @Source("gr/grnet/pithos/resources/edit_user.png")\r
86                 ImageResource permUser();\r
87 \r
88                 @Source("gr/grnet/pithos/resources/groups22.png")\r
89                 ImageResource permGroup();\r
90 \r
91                 @Source("gr/grnet/pithos/resources/delete.gif")\r
92                 ImageResource delete();\r
93         }\r
94 \r
95         final File file;\r
96 \r
97     Images images = GWT.create(Images.class);\r
98 \r
99         /**\r
100          * The widget's constructor.\r
101          */\r
102         public FilePermissionsDialog(Pithos _app, File _file) {\r
103         super(_app);\r
104         file = _file;\r
105 \r
106                 Anchor close = new Anchor("close");\r
107                 close.addStyleName("close");\r
108                 close.addClickHandler(new ClickHandler() {\r
109                         \r
110                         @Override\r
111                         public void onClick(ClickEvent event) {\r
112                                 hide();\r
113                         }\r
114                 });\r
115                 // Set the dialog's caption.\r
116                 setText("File permissions");\r
117                 setAnimationEnabled(true);\r
118                 setGlassEnabled(true);\r
119                 setStyleName("pithos-DialogBox");\r
120 \r
121                 // Outer contains inner and buttons.\r
122                 final VerticalPanel outer = new VerticalPanel();\r
123                 outer.add(close);\r
124                 final FocusPanel focusPanel = new FocusPanel(outer);\r
125                 // Inner contains generalPanel and permPanel.\r
126                 inner = new VerticalPanel();\r
127                 inner.addStyleName("inner");\r
128 \r
129         inner.add(createSharingPanel());\r
130 \r
131         outer.add(inner);\r
132 \r
133                 final Button ok = new Button("OK", new ClickHandler() {\r
134                         @Override\r
135                         public void onClick(ClickEvent event) {\r
136                                 closeDialog();\r
137                         }\r
138                 });\r
139                 ok.addStyleName("button");\r
140 \r
141         outer.add(ok);\r
142         outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);\r
143 \r
144         focusPanel.setFocus(true);\r
145         setWidget(outer);\r
146         }\r
147 \r
148     private VerticalPanel createSharingPanel() {\r
149         VerticalPanel permPanel = new VerticalPanel();\r
150 \r
151         permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), false, new Command() {\r
152                         \r
153                         @Override\r
154                         public void execute() {\r
155                                 accept();\r
156                         }\r
157                 });\r
158         permPanel.add(permList);\r
159 \r
160         HorizontalPanel permButtons = new HorizontalPanel();\r
161         final Button addUser = new Button("Add User", new ClickHandler() {\r
162             @Override\r
163             public void onClick(ClickEvent event) {\r
164                 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);\r
165                 dlg.center();\r
166                 permList.updatePermissionTable();\r
167             }\r
168         });\r
169         addUser.addStyleName("button");\r
170         permButtons.add(addUser);\r
171         permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);\r
172 \r
173         Button add = new Button("Add Group", new ClickHandler() {\r
174             @Override\r
175             public void onClick(ClickEvent event) {\r
176                 if (app.getAccount().getGroups().isEmpty()) {\r
177                     new GroupCreateDialog(app, new Command() {\r
178                                                 \r
179                                                 @Override\r
180                                                 public void execute() {\r
181                                         if (app.getAccount().getGroups().isEmpty())\r
182                                                 return;\r
183                                         PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);\r
184                                         dlg.center();\r
185                                         permList.updatePermissionTable();\r
186                                                 }\r
187                                         }).center();\r
188                 }\r
189                 else {\r
190                         PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);\r
191                         dlg.center();\r
192                         permList.updatePermissionTable();\r
193                 }\r
194             }\r
195         });\r
196         add.addStyleName("button");\r
197         permButtons.add(add);\r
198         permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);\r
199 \r
200         permButtons.setSpacing(8);\r
201         permButtons.addStyleName("pithos-TabPanelBottom");\r
202         permPanel.add(permButtons);\r
203 \r
204         final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +\r
205                     " by everyone. By checking this option, you are certifying that you have the right to " +\r
206                     "distribute this file and that it does not violate the Terms of Use.", true);\r
207         readForAllNote.setStylePrimaryName("pithos-readForAllNote");\r
208 \r
209         pathPanel = new HorizontalPanel();\r
210         pathPanel.setVisible(false);\r
211         pathPanel.setWidth("100%");\r
212         pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);\r
213         pathPanel.add(new Label("Link"));\r
214         pathPanel.setSpacing(8);\r
215         pathPanel.addStyleName("pithos-TabPanelBottom");\r
216 \r
217         path = new TextBox();\r
218         path.setWidth("100%");\r
219         path.addClickHandler(new ClickHandler() {\r
220             @Override\r
221             public void onClick(ClickEvent event) {\r
222                 Pithos.enableIESelection();\r
223                 ((TextBox) event.getSource()).selectAll();\r
224                 Pithos.preventIESelection();\r
225             }\r
226         });\r
227         path.setText(Window.Location.getHost() + file.getPublicUri());\r
228         path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");\r
229         path.setWidth("100%");\r
230         path.setReadOnly(true);\r
231         pathPanel.add(path);\r
232         permPanel.add(pathPanel);\r
233 \r
234         Scheduler.get().scheduleDeferred(new Command() {\r
235                         \r
236                         @Override\r
237                         public void execute() {\r
238                                 showLinkIfShared();\r
239                         }\r
240                 });\r
241         return permPanel;\r
242     }\r
243 \r
244     void showLinkIfShared() {\r
245                 if (file.isShared()) {\r
246                         UrlBuilder b = Window.Location.createUrlBuilder();\r
247                         b.setPath(app.getApiPath() + file.getOwner() + file.getUri());\r
248                         String href = Window.Location.getHref();\r
249                         boolean hasParameters = href.contains("?");\r
250                         path.setText(href + (hasParameters ? "&" : "?") + "goto=" + b.buildString());\r
251                 pathPanel.setVisible(true);\r
252                 }\r
253                 else {\r
254                         pathPanel.setVisible(false);\r
255                 }\r
256     }\r
257         /**\r
258          * Accepts any change and updates the file\r
259          *\r
260          */\r
261         @Override\r
262         protected boolean accept() {\r
263         updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", permList.getPermissions());\r
264         return true;\r
265         }\r
266 \r
267         protected void updateMetaData(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) {\r
268         if (newPermissions != null) {\r
269             PostRequest updateFile = new PostRequest(api, owner, path) {\r
270                 @Override\r
271                 public void onSuccess(Resource result) {\r
272                         HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwner(), path, file) {\r
273 \r
274                                                 @Override\r
275                                                 public void onSuccess(File _result) {\r
276                                                         showLinkIfShared();\r
277                                                         if (!app.isMySharedSelected())\r
278                                             app.updateFolder(file.getParent(), true, new Command() {\r
279                                                                         \r
280                                                                         @Override\r
281                                                                         public void execute() {\r
282                                                                                 app.updateMySharedRoot();\r
283                                                                         }\r
284                                                                 }, true);\r
285                                                         else\r
286                                                                 app.updateSharedFolder(file.getParent(), true);\r
287                                                 }\r
288 \r
289                                                 @Override\r
290                                                 public void onError(Throwable t) {\r
291                                     GWT.log("", t);\r
292                                                         app.setError(t);\r
293                                     app.displayError("System error modifying file:" + t.getMessage());\r
294                                                 }\r
295 \r
296                                                 @Override\r
297                                                 protected void onUnauthorized(Response response) {\r
298                                                         app.sessionExpired();\r
299                                                 }\r
300                                         };\r
301                                         headFile.setHeader("X-Auth-Token", app.getToken());\r
302                                         Scheduler.get().scheduleDeferred(headFile);\r
303                 }\r
304 \r
305                 @Override\r
306                 public void onError(Throwable t) {\r
307                     GWT.log("", t);\r
308                                         app.setError(t);\r
309                     app.displayError("System error modifying file:" + t.getMessage());\r
310                 }\r
311 \r
312                                 @Override\r
313                                 protected void onUnauthorized(Response response) {\r
314                                         app.sessionExpired();\r
315                                 }\r
316             };\r
317             updateFile.setHeader("X-Auth-Token", app.getToken());\r
318             \r
319             String readPermHeader = "read=";\r
320             String writePermHeader = "write=";\r
321             for (String u : newPermissions.keySet()) {\r
322                 Boolean[] p = newPermissions.get(u);\r
323                 if (p[0] != null && p[0])\r
324                     readPermHeader += u + ",";\r
325                 if (p[1] != null && p[1])\r
326                     writePermHeader += u + ",";\r
327             }\r
328             if (readPermHeader.endsWith("="))\r
329                 readPermHeader = "";\r
330             else if (readPermHeader.endsWith(","))\r
331                 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);\r
332             if (writePermHeader.endsWith("="))\r
333                 writePermHeader = "";\r
334             else if (writePermHeader.endsWith(","))\r
335                 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);\r
336             String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;\r
337             if (permHeader.length() == 0)\r
338                 permHeader="~";\r
339             else\r
340                 permHeader = URL.encodePathSegment(permHeader);\r
341             updateFile.setHeader("X-Object-Sharing", permHeader);\r
342             Scheduler.get().scheduleDeferred(updateFile);\r
343         }\r
344         else if (!app.isMySharedSelected())\r
345             app.updateFolder(file.getParent(), true, new Command() {\r
346                                 \r
347                                 @Override\r
348                                 public void execute() {\r
349                                         if (file.isSharedOrPublished())\r
350                                                 app.updateMySharedRoot();\r
351                                 }\r
352                         }, true);\r
353         else\r
354                 app.updateSharedFolder(file.getParent(), true);\r
355     }\r
356 \r
357         @Override\r
358         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
359             super.onPreviewNativeEvent(preview);\r
360 \r
361             NativeEvent evt = preview.getNativeEvent();\r
362             if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)\r
363                                 closeDialog();\r
364         }\r
365 }\r