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.Resource;
\r
39 import gr.grnet.pithos.web.client.rest.HeadRequest;
\r
40 import gr.grnet.pithos.web.client.rest.PostRequest;
\r
42 import java.util.Map;
\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
69 * The 'File properties' dialog box implementation.
\r
72 public class FilePermissionsDialog extends AbstractPropertiesDialog {
\r
74 protected PermissionsList permList;
\r
76 private HorizontalPanel pathPanel;
\r
78 private TextBox path;
\r
81 * An image bundle for this widgets images.
\r
83 public interface Images extends MessagePanel.Images {
\r
85 @Source("gr/grnet/pithos/resources/edit_user.png")
\r
86 ImageResource permUser();
\r
88 @Source("gr/grnet/pithos/resources/groups22.png")
\r
89 ImageResource permGroup();
\r
91 @Source("gr/grnet/pithos/resources/editdelete.png")
\r
92 ImageResource delete();
\r
97 Images images = GWT.create(Images.class);
\r
100 * The widget's constructor.
\r
102 public FilePermissionsDialog(Pithos _app, File _file) {
\r
106 Anchor close = new Anchor();
\r
107 close.addStyleName("close");
\r
108 close.addClickHandler(new ClickHandler() {
\r
111 public void onClick(ClickEvent event) {
\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
121 // Outer contains inner and buttons.
\r
122 final VerticalPanel outer = new VerticalPanel();
\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
129 inner.add(createSharingPanel());
\r
133 final Button ok = new Button("Close", new ClickHandler() {
\r
135 public void onClick(ClickEvent event) {
\r
139 ok.addStyleName("button");
\r
142 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
\r
144 focusPanel.setFocus(true);
\r
148 private VerticalPanel createSharingPanel() {
\r
149 VerticalPanel permPanel = new VerticalPanel();
\r
151 permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), false, new Command() {
\r
154 public void execute() {
\r
158 permPanel.add(permList);
\r
160 HorizontalPanel permButtons = new HorizontalPanel();
\r
161 final Button addUser = new Button("Add User", new ClickHandler() {
\r
163 public void onClick(ClickEvent event) {
\r
164 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
\r
166 permList.updatePermissionTable();
\r
169 addUser.addStyleName("button");
\r
170 permButtons.add(addUser);
\r
171 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
173 Button add = new Button("Add Group", new ClickHandler() {
\r
175 public void onClick(ClickEvent event) {
\r
176 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
\r
178 permList.updatePermissionTable();
\r
181 add.addStyleName("button");
\r
182 permButtons.add(add);
\r
183 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
185 permButtons.setSpacing(8);
\r
186 permButtons.addStyleName("pithos-TabPanelBottom");
\r
187 permPanel.add(permButtons);
\r
189 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
\r
190 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
191 "distribute this file and that it does not violate the Terms of Use.", true);
\r
192 readForAllNote.setStylePrimaryName("pithos-readForAllNote");
\r
194 pathPanel = new HorizontalPanel();
\r
195 pathPanel.setVisible(false);
\r
196 pathPanel.setWidth("100%");
\r
197 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
198 pathPanel.add(new Label("Link"));
\r
199 pathPanel.setSpacing(8);
\r
200 pathPanel.addStyleName("pithos-TabPanelBottom");
\r
202 path = new TextBox();
\r
203 path.setWidth("100%");
\r
204 path.addClickHandler(new ClickHandler() {
\r
206 public void onClick(ClickEvent event) {
\r
207 Pithos.enableIESelection();
\r
208 ((TextBox) event.getSource()).selectAll();
\r
209 Pithos.preventIESelection();
\r
212 path.setText(Window.Location.getHost() + file.getPublicUri());
\r
213 path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
214 path.setWidth("100%");
\r
215 path.setReadOnly(true);
\r
216 pathPanel.add(path);
\r
217 permPanel.add(pathPanel);
\r
219 Scheduler.get().scheduleDeferred(new Command() {
\r
222 public void execute() {
\r
223 showLinkIfShared();
\r
229 void showLinkIfShared() {
\r
230 if (file.isShared()) {
\r
231 UrlBuilder b = Window.Location.createUrlBuilder();
\r
232 b.setPath(app.getApiPath() + file.getOwner() + file.getUri());
\r
233 String href = Window.Location.getHref();
\r
234 boolean hasParameters = href.contains("?");
\r
235 path.setText(href + (hasParameters ? "&" : "?") + "goto=" + b.buildString());
\r
236 pathPanel.setVisible(true);
\r
239 pathPanel.setVisible(false);
\r
243 * Accepts any change and updates the file
\r
247 protected void accept() {
\r
248 updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", permList.getPermissions());
\r
251 protected void updateMetaData(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) {
\r
252 if (newPermissions != null) {
\r
253 PostRequest updateFile = new PostRequest(api, owner, path) {
\r
255 public void onSuccess(Resource result) {
\r
256 HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwner(), path, file) {
\r
259 public void onSuccess(File _result) {
\r
260 showLinkIfShared();
\r
261 app.updateFolder(file.getParent(), true, new Command() {
\r
264 public void execute() {
\r
265 app.updateMySharedRoot();
\r
271 public void onError(Throwable t) {
\r
274 app.displayError("System error modifying file:" + t.getMessage());
\r
278 protected void onUnauthorized(Response response) {
\r
279 app.sessionExpired();
\r
282 headFile.setHeader("X-Auth-Token", app.getToken());
\r
283 Scheduler.get().scheduleDeferred(headFile);
\r
287 public void onError(Throwable t) {
\r
290 app.displayError("System error modifying file:" + t.getMessage());
\r
294 protected void onUnauthorized(Response response) {
\r
295 app.sessionExpired();
\r
298 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
300 String readPermHeader = "read=";
\r
301 String writePermHeader = "write=";
\r
302 for (String u : newPermissions.keySet()) {
\r
303 Boolean[] p = newPermissions.get(u);
\r
304 if (p[0] != null && p[0])
\r
305 readPermHeader += u + ",";
\r
306 if (p[1] != null && p[1])
\r
307 writePermHeader += u + ",";
\r
309 if (readPermHeader.endsWith("="))
\r
310 readPermHeader = "";
\r
311 else if (readPermHeader.endsWith(","))
\r
312 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
\r
313 if (writePermHeader.endsWith("="))
\r
314 writePermHeader = "";
\r
315 else if (writePermHeader.endsWith(","))
\r
316 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
\r
317 String permHeader = readPermHeader + ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? ";" : "") + writePermHeader;
\r
318 if (permHeader.length() == 0)
\r
321 permHeader = URL.encodePathSegment(permHeader);
\r
322 updateFile.setHeader("X-Object-Sharing", permHeader);
\r
323 Scheduler.get().scheduleDeferred(updateFile);
\r
326 app.updateFolder(file.getParent(), true, new Command() {
\r
329 public void execute() {
\r
330 if (file.isSharedOrPublished())
\r
331 app.updateMySharedRoot();
\r
337 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
338 super.onPreviewNativeEvent(preview);
\r
340 NativeEvent evt = preview.getNativeEvent();
\r
341 if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)
\r