3ea371f5ee40ccb74d7be20ab05dab7e92c05ada
[pithos-web-client] / src / gr / grnet / pithos / web / client / FilePublishDialog.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 FilePublishDialog extends AbstractPropertiesDialog {\r
73 \r
74         protected CheckBox readForAll;\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/editdelete.png")\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 FilePublishDialog(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("Publish/Un-publish");\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         final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +\r
152                     " by everyone. By checking this option, you are certifying that you have the right to " +\r
153                     "distribute this file and that it does not violate the Terms of Use.", true);\r
154         readForAllNote.setStylePrimaryName("pithos-readForAllNote");\r
155 \r
156         readForAll = new CheckBox();\r
157         readForAll.setValue(file.isPublished());\r
158         readForAll.addClickHandler(new ClickHandler() {\r
159             @Override\r
160             public void onClick(ClickEvent event) {\r
161                 accept();\r
162             }\r
163         });\r
164 \r
165         // Only show the read for all permission if the user is the owner.\r
166         if (file.getOwner().equals(app.getUsername())) {\r
167             final HorizontalPanel permForAll = new HorizontalPanel();\r
168             permForAll.add(new Label("Public"));\r
169             permForAll.add(readForAll);\r
170             permForAll.setSpacing(8);\r
171             permForAll.addStyleName("pithos-TabPanelBottom");\r
172             permForAll.add(readForAllNote);\r
173             permPanel.add(permForAll);\r
174         }\r
175 \r
176         pathPanel = new HorizontalPanel();\r
177         pathPanel.setVisible(false);\r
178         pathPanel.setWidth("100%");\r
179         pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);\r
180         pathPanel.add(new Label("Link"));\r
181         pathPanel.setSpacing(8);\r
182         pathPanel.addStyleName("pithos-TabPanelBottom");\r
183 \r
184         path = new TextBox();\r
185         path.setWidth("100%");\r
186         path.addClickHandler(new ClickHandler() {\r
187             @Override\r
188             public void onClick(ClickEvent event) {\r
189                 Pithos.enableIESelection();\r
190                 ((TextBox) event.getSource()).selectAll();\r
191                 Pithos.preventIESelection();\r
192             }\r
193         });\r
194         path.setText(Window.Location.getHost() + file.getPublicUri());\r
195         path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");\r
196         path.setWidth("100%");\r
197         path.setReadOnly(true);\r
198         pathPanel.add(path);\r
199         permPanel.add(pathPanel);\r
200 \r
201         Scheduler.get().scheduleDeferred(new Command() {\r
202                         \r
203                         @Override\r
204                         public void execute() {\r
205                                 showLinkIfPublished();\r
206                         }\r
207                 });\r
208         return permPanel;\r
209     }\r
210 \r
211     void showLinkIfPublished() {\r
212                 if (file.isPublished()) {\r
213                         UrlBuilder b = Window.Location.createUrlBuilder();\r
214                         b.setPath(file.getPublicUri());\r
215                         path.setText(b.buildString());\r
216                 pathPanel.setVisible(true);\r
217                 }\r
218                 else {\r
219                         pathPanel.setVisible(false);\r
220                 }\r
221     }\r
222         /**\r
223          * Accepts any change and updates the file\r
224          * @return \r
225          *\r
226          */\r
227         @Override\r
228         protected boolean accept() {\r
229         Boolean published = null;\r
230                 if (readForAll.getValue() != file.isPublished())\r
231                         if (file.getOwner().equals(app.getUsername()))\r
232                 published = readForAll.getValue();\r
233         updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", published);\r
234         return true;\r
235         }\r
236 \r
237         protected void updateMetaData(String api, String owner, final String path, final Boolean published) {\r
238         if (published != null) {\r
239             PostRequest updateFile = new PostRequest(api, owner, path) {\r
240                 @Override\r
241                 public void onSuccess(Resource result) {\r
242                         HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwner(), path, file) {\r
243 \r
244                                                 @Override\r
245                                                 public void onSuccess(File _result) {\r
246                                                         showLinkIfPublished();\r
247                                                         if (!app.isMySharedSelected())\r
248                                             app.updateFolder(file.getParent(), true, new Command() {\r
249                                                                         \r
250                                                                         @Override\r
251                                                                         public void execute() {\r
252                                                                                 app.updateMySharedRoot();\r
253                                                                         }\r
254                                                                 }, true);\r
255                                                         else\r
256                                                                 app.updateSharedFolder(file.getParent(), true);\r
257                                                 }\r
258 \r
259                                                 @Override\r
260                                                 public void onError(Throwable t) {\r
261                                     GWT.log("", t);\r
262                                                         app.setError(t);\r
263                                     app.displayError("System error modifying file:" + t.getMessage());\r
264                                                 }\r
265 \r
266                                                 @Override\r
267                                                 protected void onUnauthorized(Response response) {\r
268                                                         app.sessionExpired();\r
269                                                 }\r
270                                         };\r
271                                         headFile.setHeader("X-Auth-Token", app.getToken());\r
272                                         Scheduler.get().scheduleDeferred(headFile);\r
273                 }\r
274 \r
275                 @Override\r
276                 public void onError(Throwable t) {\r
277                     GWT.log("", t);\r
278                                         app.setError(t);\r
279                     app.displayError("System error modifying file:" + t.getMessage());\r
280                 }\r
281 \r
282                                 @Override\r
283                                 protected void onUnauthorized(Response response) {\r
284                                         app.sessionExpired();\r
285                                 }\r
286             };\r
287             updateFile.setHeader("X-Auth-Token", app.getToken());\r
288             updateFile.setHeader("X-Object-Public", published.toString());\r
289             Scheduler.get().scheduleDeferred(updateFile);\r
290         }\r
291         else if (!app.isMySharedSelected())\r
292             app.updateFolder(file.getParent(), true, new Command() {\r
293                                 \r
294                                 @Override\r
295                                 public void execute() {\r
296                                         if (file.isSharedOrPublished())\r
297                                                 app.updateMySharedRoot();\r
298                                 }\r
299                         }, true);\r
300         else\r
301                 app.updateSharedFolder(file.getParent(), true);\r
302     }\r
303 \r
304         @Override\r
305         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
306             super.onPreviewNativeEvent(preview);\r
307 \r
308             NativeEvent evt = preview.getNativeEvent();\r
309             if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)\r
310                                 closeDialog();\r
311         }\r
312 }\r