2 * Copyright 2011-2013 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client;
37 import com.google.gwt.http.client.URL;
38 import com.google.gwt.user.client.ui.*;
39 import gr.grnet.pithos.web.client.foldertree.File;
40 import gr.grnet.pithos.web.client.rest.HeadRequest;
41 import gr.grnet.pithos.web.client.rest.PostRequest;
43 import com.google.gwt.core.client.GWT;
44 import com.google.gwt.core.client.Scheduler;
45 import com.google.gwt.dom.client.NativeEvent;
46 import com.google.gwt.event.dom.client.ClickEvent;
47 import com.google.gwt.event.dom.client.ClickHandler;
48 import com.google.gwt.event.dom.client.KeyCodes;
49 import com.google.gwt.http.client.Response;
50 import com.google.gwt.http.client.UrlBuilder;
51 import com.google.gwt.resources.client.ImageResource;
52 import com.google.gwt.user.client.Command;
53 import com.google.gwt.user.client.Window;
54 import com.google.gwt.user.client.Event.NativePreviewEvent;
59 * UI for the "Share" command.
61 public class FileShareDialog extends AbstractPropertiesDialog {
62 private static final class PermissionsUncheckWarning extends AbstractPropertiesDialog {
63 private PermissionsUncheckWarning(Pithos app) {
65 final Anchor close = new Anchor("close");
66 close.addStyleName("close");
67 close.addClickHandler(new ClickHandler() {
69 public void onClick(ClickEvent event) {
73 final String dialogText = "Info";
75 setStyleName("pithos-DialogBox");
77 final VerticalPanel panel = new VerticalPanel();
80 VerticalPanel inner = new VerticalPanel();
81 inner.addStyleName("inner");
85 "It seems you are already sharing this file with some users." +
87 "Please remove all users from the sharing list, to be able to uncheck this option."
91 final Button ok = new Button("OK", new ClickHandler() {
93 public void onClick(ClickEvent event) {
98 ok.addStyleName("button");
102 panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
108 protected boolean accept() {
113 // For public sharing
114 private final HorizontalPanel publicPathPanel = new HorizontalPanel();
115 private final TextBox publicPathText = new TextBox();
117 // For private sharing
118 private final HorizontalPanel privatePathPanel = new HorizontalPanel();
119 private final TextBox privatePathText = new TextBox();
120 private PermissionsList permList;
122 public interface PrivateSharingImages extends MessagePanel.Images {
124 @Source("gr/grnet/pithos/resources/edit_user.png")
125 ImageResource permUser();
127 @Source("gr/grnet/pithos/resources/groups22.png")
128 ImageResource permGroup();
130 @Source("gr/grnet/pithos/resources/delete.gif")
131 ImageResource delete();
134 private final File file;
136 private final PrivateSharingImages privateSharingImages = GWT.create(PrivateSharingImages.class);
139 * The widget's constructor.
141 public FileShareDialog(Pithos app, File file) {
145 Anchor close = new Anchor("close");
146 close.addStyleName("close");
147 close.addClickHandler(new ClickHandler() {
150 public void onClick(ClickEvent event) {
154 // Set the dialog's caption.
155 setText(Const.TXT_SHARE_FILE);
156 setGlassEnabled(true);
157 setStyleName("pithos-DialogBox");
159 // Outer contains inner and buttons.
160 final VerticalPanel outer = new VerticalPanel();
162 final FocusPanel focusPanel = new FocusPanel(outer);
163 // Inner contains generalPanel and permPanel.
164 inner = new VerticalPanel();
165 inner.addStyleName("inner");
167 inner.add(createMainPanel());
171 final Button ok = new Button("OK", new ClickHandler() {
173 public void onClick(ClickEvent event) {
177 ok.addStyleName("button");
180 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
182 focusPanel.setFocus(true);
186 private boolean IamOwner() {
187 return file.getOwnerID().equals(app.getUserID());
190 private void populatePublicSharingPanel(VerticalPanel publicSharingPanel) {
192 final HorizontalPanel publicCheckPanel = new HorizontalPanel();
193 publicCheckPanel.setSpacing(8);
196 final CheckBox publicCheckBox = new CheckBox();
197 Label publicCheckTitle = new InlineHTML("<b>Public on the Internet</b>");
198 Label publicCheckInfo = new Label("Anyone who has the public link can access. No sign-in required.", true);
200 publicCheckBox.setValue(isFilePubliclyShared());
201 publicCheckBox.addClickHandler(new ClickHandler() {
203 public void onClick(ClickEvent event) {
204 final Boolean published;
205 if(publicCheckBox.getValue() != file.isPublished() && IamOwner()) {
206 published = publicCheckBox.getValue();
209 published = Boolean.FALSE;
212 updateMetaDataForPublicSharing(published);
216 publicCheckPanel.add(publicCheckBox);
217 publicCheckPanel.add(publicCheckTitle);
218 publicCheckPanel.add(publicCheckInfo);
221 publicPathPanel.setVisible(false);
222 publicPathPanel.setWidth(Const.PERCENT_100);
223 publicPathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
224 publicPathPanel.add(new Label(Const.TXT_PUBLIC_LINK));
225 publicPathPanel.setSpacing(8);
226 publicPathPanel.addStyleName("pithos-TabPanelBottom");
228 publicPathText.setWidth(Const.PERCENT_100);
229 publicPathText.addClickHandler(new ClickHandler() {
231 public void onClick(ClickEvent event) {
232 Pithos.enableIESelection();
233 ((TextBox) event.getSource()).selectAll();
234 Pithos.preventIESelection();
237 publicPathText.setText(Window.Location.getHost() + file.getPublicUri());
238 publicPathText.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
239 publicPathText.setReadOnly(true);
240 publicPathPanel.add(publicPathText);
242 publicSharingPanel.add(publicCheckPanel);
243 publicSharingPanel.add(publicPathPanel);
245 Scheduler.get().scheduleDeferred(new Command() {
247 public void execute() {
248 showLinkForPublicSharing();
254 private void populatePrivateSharingPanel(VerticalPanel privateSharingPanel) {
255 final HorizontalPanel privateCheckPanel = new HorizontalPanel();
256 final VerticalPanel privatePermPanel = new VerticalPanel();
257 final HorizontalPanel permButtons = new HorizontalPanel();
259 privateCheckPanel.setSpacing(8);
260 privatePermPanel.setVisible(isFilePrivatelyShared());
261 permButtons.setSpacing(8);
264 final CheckBox privateCheckBox = new CheckBox();
265 final Label privateCheckTitle = new InlineHTML("<b>Private sharing</b>");
266 final Label privateCheckInfo = new Label("Only people explicitly granted permission can access. Sign-in required.", true);
268 privateCheckBox.setValue(isFilePrivatelyShared());
269 privateCheckBox.addClickHandler(new ClickHandler() {
271 public void onClick(ClickEvent event) {
272 // This is the value *after* the click is applied :)
273 boolean userCheckedIt = privateCheckBox.getValue();
274 boolean userUncheckedIt = !userCheckedIt;
276 if(isFilePrivatelyShared()) {
277 // ignore the click, set it always to "checked"
278 privateCheckBox.setValue(true);
280 privatePermPanel.setVisible(true);
283 if(userUncheckedIt) {
284 new PermissionsUncheckWarning(app).center();
288 privatePermPanel.setVisible(userCheckedIt);
293 privateCheckPanel.add(privateCheckBox);
294 privateCheckPanel.add(privateCheckTitle);
295 privateCheckPanel.add(privateCheckInfo);
298 permList = new PermissionsList(app, privateSharingImages, file.getPermissions(), file.getOwnerID(), false, new Command() {
300 public void execute() {
301 updateMetaDataForPrivateSharing();
305 // Permission Add buttons
306 Button addUser = new Button("Add User", new ClickHandler() {
308 public void onClick(ClickEvent event) {
309 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
311 permList.updatePermissionTable();
314 addUser.addStyleName("button");
315 permButtons.add(addUser);
317 final boolean haveGroups = app.getAccount().getGroups().size() > 0;
320 Button addGroup = new Button("Add Group", new ClickHandler() {
322 public void onClick(ClickEvent event) {
323 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
325 permList.updatePermissionTable();
328 addGroup.addStyleName("button");
330 permButtons.add(addGroup);
331 permButtons.setCellHorizontalAlignment(addGroup, HasHorizontalAlignment.ALIGN_CENTER);
334 final Button addEverybody = new Button("Add everybody", new ClickHandler() {
336 public void onClick(ClickEvent event) {
337 Pithos.LOG("Adding to Everybody");
339 permList.addPermission("*", true, false);
340 permList.updatePermissionTable();
343 addEverybody.addStyleName("button");
344 permButtons.add(addEverybody);
346 privatePermPanel.add(permList);
347 privatePermPanel.add(permButtons);
350 privatePathPanel.setVisible(false);
351 privatePathPanel.setWidth(Const.PERCENT_100);
352 privatePathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
353 privatePathPanel.add(new Label(Const.TXT_PRIVATE_LINK));
354 privatePathPanel.setSpacing(8);
355 privatePathPanel.addStyleName("pithos-TabPanelBottom");
357 privatePathText.setWidth(Const.PERCENT_100);
358 privatePathText.addClickHandler(new ClickHandler() {
360 public void onClick(ClickEvent event) {
361 Pithos.enableIESelection();
362 ((TextBox) event.getSource()).selectAll();
363 Pithos.preventIESelection();
366 privatePathText.setText(Window.Location.getHost() + file.getPublicUri());
367 privatePathText.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
368 privatePathText.setWidth(Const.PERCENT_100);
369 privatePathText.setReadOnly(true);
370 privatePathPanel.add(privatePathText);
372 Scheduler.get().scheduleDeferred(new Command() {
374 public void execute() {
375 showLinkForPrivateSharing();
379 privateSharingPanel.add(privateCheckPanel);
380 privateSharingPanel.add(privatePermPanel);
381 privateSharingPanel.add(privatePathPanel);
384 private Panel createMainPanel() {
385 VerticalPanel panelAll = new VerticalPanel();
386 VerticalPanel panelPublic = new VerticalPanel();
387 VerticalPanel panelPrivate = new VerticalPanel();
389 populatePrivateSharingPanel(panelPrivate);
390 populatePublicSharingPanel(panelPublic);
392 panelAll.add(panelPrivate);
393 panelAll.add(new InlineHTML("<hr/>"));
394 panelAll.add(panelPublic);
399 private boolean isFilePubliclyShared() {
400 return file.isPublished();
403 private boolean isFilePrivatelyShared() {
404 return file.isShared();
407 private void showLinkForPublicSharing() {
408 if (isFilePubliclyShared()) {
409 UrlBuilder b = Window.Location.createUrlBuilder();
410 b.setPath(file.getPublicUri());
411 publicPathText.setText(b.buildString());
412 publicPathPanel.setVisible(true);
415 publicPathPanel.setVisible(false);
419 private void showLinkForPrivateSharing() {
420 if (isFilePrivatelyShared()) {
421 UrlBuilder b = Window.Location.createUrlBuilder();
422 b.setPath(Pithos.getStorageAPIURL() + file.getOwnerID() + file.getUri());
423 String href = Window.Location.getHref();
424 boolean hasParameters = href.contains(Const.QUESTION_MARK);
425 privatePathText.setText(href + (hasParameters ? Const.AMPERSAND : Const.QUESTION_MARK) + Const.GOTO_EQ + b.buildString());
426 privatePathPanel.setVisible(true);
429 privatePathPanel.setVisible(false);
433 protected void updateMetaDataForPublicSharing(String api, String owner, final String path, final Boolean published) {
434 if (published != null) {
435 PostRequest updateFile = new PostRequest(api, owner, path) {
437 public void onSuccess(Resource result) {
438 HeadRequest<File> headFile = new HeadRequest<File>(File.class, Pithos.getStorageAPIURL(), file.getOwnerID(), path, file) {
441 public void onSuccess(File _result) {
442 showLinkForPublicSharing();
443 if (!app.isMySharedSelected()) {
444 app.updateFolder(file.getParent(), true, new Command() {
447 public void execute() {
448 app.updateMySharedRoot();
453 app.updateSharedFolder(file.getParent(), true);
458 public void onError(Throwable t) {
460 app.displayError("System error modifying file:" + t.getMessage());
464 protected void onUnauthorized(Response response) {
465 app.sessionExpired();
468 headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
469 Scheduler.get().scheduleDeferred(headFile);
473 public void onError(Throwable t) {
475 app.displayError("System error modifying file:" + t.getMessage());
479 protected void onUnauthorized(Response response) {
480 app.sessionExpired();
483 updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
484 updateFile.setHeader(Const.X_OBJECT_PUBLIC, published.toString());
485 Scheduler.get().scheduleDeferred(updateFile);
487 else if (!app.isMySharedSelected())
488 app.updateFolder(file.getParent(), true, new Command() {
490 public void execute() {
491 if (file.isSharedOrPublished()) {
492 app.updateMySharedRoot();
497 app.updateSharedFolder(file.getParent(), true);
499 protected void updateMetaDataForPublicSharing(Boolean published) {
500 updateMetaDataForPublicSharing(
501 Pithos.getStorageAPIURL(),
503 file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
508 protected void updateMetaDataForPrivateSharing(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) {
509 if (newPermissions != null) {
510 PostRequest updateFile = new PostRequest(api, owner, path) {
512 public void onSuccess(Resource result) {
513 HeadRequest<File> headFile = new HeadRequest<File>(File.class, Pithos.getStorageAPIURL(), file.getOwnerID(), path, file) {
516 public void onSuccess(File _result) {
517 showLinkForPrivateSharing();
518 if (!app.isMySharedSelected())
519 app.updateFolder(file.getParent(), true, new Command() {
522 public void execute() {
523 app.updateMySharedRoot();
527 app.updateSharedFolder(file.getParent(), true);
531 public void onError(Throwable t) {
533 app.displayError("System error modifying file:" + t.getMessage());
537 protected void onUnauthorized(Response response) {
538 app.sessionExpired();
541 headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
542 Scheduler.get().scheduleDeferred(headFile);
546 public void onError(Throwable t) {
548 app.displayError("System error modifying file:" + t.getMessage());
552 protected void onUnauthorized(Response response) {
553 app.sessionExpired();
556 updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
558 String readPermHeader = Const.READ_EQ;
559 String writePermHeader = Const.WRITE_EQ;
560 for (String u : newPermissions.keySet()) {
561 Boolean[] p = newPermissions.get(u);
562 if (p[0] != null && p[0]) {
563 readPermHeader += u + Const.COMMA;
565 if (p[1] != null && p[1]) {
566 writePermHeader += u + Const.COMMA;
569 if (readPermHeader.endsWith(Const.EQ)) {
572 else if (readPermHeader.endsWith(Const.COMMA)) {
573 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
575 if (writePermHeader.endsWith(Const.EQ)) {
576 writePermHeader = "";
578 else if (writePermHeader.endsWith(Const.COMMA)) {
579 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
581 String permHeader = readPermHeader +
582 ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? Const.SEMI : "") + writePermHeader;
583 if (permHeader.length() == 0) {
584 permHeader=Const.TILDE;
587 permHeader = URL.encodePathSegment(permHeader);
589 updateFile.setHeader(Const.X_OBJECT_SHARING, permHeader);
590 Scheduler.get().scheduleDeferred(updateFile);
592 else if (!app.isMySharedSelected()) {
593 app.updateFolder(file.getParent(), true, new Command() {
595 public void execute() {
596 if (file.isSharedOrPublished())
597 app.updateMySharedRoot();
602 app.updateSharedFolder(file.getParent(), true);
605 protected void updateMetaDataForPrivateSharing() {
606 updateMetaDataForPrivateSharing(
607 Pithos.getStorageAPIURL(),
609 file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
610 permList.getPermissions()
615 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
616 super.onPreviewNativeEvent(preview);
618 NativeEvent evt = preview.getNativeEvent();
619 if (evt.getType().equals(Const.EVENT_TYPE_KEYDOWN) &&
620 evt.getKeyCode() == KeyCodes.KEY_ENTER) {
627 protected boolean accept() {