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 {
63 private final HorizontalPanel publicPathPanel = new HorizontalPanel();
64 private final TextBox publicPathText = new TextBox();
66 // For private sharing
67 private final HorizontalPanel privatePathPanel = new HorizontalPanel();
68 private final TextBox privatePathText = new TextBox();
69 private final VerticalPanel privatePermSuperPanel = new VerticalPanel();
70 private PermissionsList permList;
73 * An image bundle for this widgets images.
75 public interface PublicSharingImages extends MessagePanel.Images {
77 @Source("gr/grnet/pithos/resources/edit_user.png")
78 ImageResource permUser();
80 @Source("gr/grnet/pithos/resources/groups22.png")
81 ImageResource permGroup();
83 @Source("gr/grnet/pithos/resources/editdelete.png")
84 ImageResource delete();
87 public interface PrivateSharingImages extends MessagePanel.Images {
89 @Source("gr/grnet/pithos/resources/edit_user.png")
90 ImageResource permUser();
92 @Source("gr/grnet/pithos/resources/groups22.png")
93 ImageResource permGroup();
95 @Source("gr/grnet/pithos/resources/delete.gif")
96 ImageResource delete();
99 private final File file;
101 private final PublicSharingImages publicSharingImages = GWT.create(PublicSharingImages.class);
102 private final PrivateSharingImages privateSharingImages = GWT.create(PrivateSharingImages.class);
105 * The widget's constructor.
107 public FileShareDialog(Pithos _app, File _file) {
111 Anchor close = new Anchor("close");
112 close.addStyleName("close");
113 close.addClickHandler(new ClickHandler() {
116 public void onClick(ClickEvent event) {
120 // Set the dialog's caption.
122 setGlassEnabled(true);
123 setStyleName("pithos-DialogBox");
125 // Outer contains inner and buttons.
126 final VerticalPanel outer = new VerticalPanel();
128 final FocusPanel focusPanel = new FocusPanel(outer);
129 // Inner contains generalPanel and permPanel.
130 inner = new VerticalPanel();
131 inner.addStyleName("inner");
133 inner.add(createMainPanel());
137 final Button ok = new Button("OK", new ClickHandler() {
139 public void onClick(ClickEvent event) {
143 ok.addStyleName("button");
146 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
148 focusPanel.setFocus(true);
152 private boolean IamOwner() {
153 return file.getOwnerID().equals(app.getUserID());
156 private void populatePublicSharingPanel(VerticalPanel publicSharingPanel) {
158 final HorizontalPanel publicCheckPanel = new HorizontalPanel();
159 publicCheckPanel.setSpacing(8);
162 final CheckBox publicCheckBox = new CheckBox();
163 Label publicCheckTitle = new InlineHTML("<b>Public on the Internet</b>");
164 Label publicCheckInfo = new Label("Anyone who has the public link can access. No sign-in required.", true);
166 publicCheckBox.setValue(isFilePubliclyShared());
167 publicCheckBox.addClickHandler(new ClickHandler() {
169 public void onClick(ClickEvent event) {
170 final Boolean published;
171 if(publicCheckBox.getValue() != file.isPublished() && IamOwner()) {
172 published = publicCheckBox.getValue();
175 published = Boolean.FALSE;
178 updateMetaDataForPublicSharing(published);
182 publicCheckPanel.add(publicCheckBox);
183 publicCheckPanel.add(publicCheckTitle);
184 publicCheckPanel.add(publicCheckInfo);
187 publicPathPanel.setVisible(false);
188 publicPathPanel.setWidth(Const.PERCENT_100);
189 publicPathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
190 publicPathPanel.add(new Label("Link"));
191 publicPathPanel.setSpacing(8);
192 publicPathPanel.addStyleName("pithos-TabPanelBottom");
194 publicPathText.setWidth(Const.PERCENT_100);
195 publicPathText.addClickHandler(new ClickHandler() {
197 public void onClick(ClickEvent event) {
198 Pithos.enableIESelection();
199 ((TextBox) event.getSource()).selectAll();
200 Pithos.preventIESelection();
203 publicPathText.setText(Window.Location.getHost() + file.getPublicUri());
204 publicPathText.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
205 publicPathText.setReadOnly(true);
206 publicPathPanel.add(publicPathText);
208 publicSharingPanel.add(publicCheckPanel);
209 publicSharingPanel.add(publicPathPanel);
211 Scheduler.get().scheduleDeferred(new Command() {
213 public void execute() {
214 showLinkForPublicSharing();
220 private void populatePrivateSharingPanel(VerticalPanel privateSharingPanel) {
221 final HorizontalPanel privateCheckPanel = new HorizontalPanel();
222 final VerticalPanel privatePermPanel = new VerticalPanel();
223 final HorizontalPanel permButtons = new HorizontalPanel();
225 privateCheckPanel.setSpacing(8);
226 privatePermPanel.setVisible(isFilePrivatelyShared());
227 permButtons.setSpacing(8);
230 final CheckBox privateCheckBox = new CheckBox();
231 final Label privateCheckTitle = new InlineHTML("<b>Private sharing</b>");
232 final Label privateCheckInfo = new Label("Only people explicitly granted permission can access. Sign-in required.", true);
234 privateCheckBox.setValue(isFilePrivatelyShared());
235 privateCheckBox.addClickHandler(new ClickHandler() {
237 public void onClick(ClickEvent event) {
238 if(isFilePrivatelyShared()) {
239 // ignore the click, set it always to "checked"
240 privateCheckBox.setValue(true);
242 privatePermPanel.setVisible(true);
245 // This is the value *after* the click is applied :)
246 boolean isChecked = privateCheckBox.getValue();
247 privatePermPanel.setVisible(isChecked);
252 privateCheckPanel.add(privateCheckBox);
253 privateCheckPanel.add(privateCheckTitle);
254 privateCheckPanel.add(privateCheckInfo);
257 permList = new PermissionsList(app, privateSharingImages, file.getPermissions(), file.getOwnerID(), false, new Command() {
259 public void execute() {
260 updateMetaDataForPrivateSharing();
264 // Permission Add buttons
265 Button addUser = new Button("Add User", new ClickHandler() {
267 public void onClick(ClickEvent event) {
268 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
270 permList.updatePermissionTable();
273 addUser.addStyleName("button");
274 permButtons.add(addUser);
275 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
277 Button add = new Button("Add Group", new ClickHandler() {
279 public void onClick(ClickEvent event) {
280 if (app.getAccount().getGroups().isEmpty()) {
281 new GroupCreateDialog(app, new Command() {
284 public void execute() {
285 if (app.getAccount().getGroups().isEmpty()) {
288 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
294 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
299 add.addStyleName("button");
300 permButtons.add(add);
301 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
303 privatePermPanel.add(permList);
304 privatePermPanel.add(permButtons);
307 privatePathPanel.setVisible(false);
308 privatePathPanel.setWidth(Const.PERCENT_100);
309 privatePathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
310 privatePathPanel.add(new Label("Link"));
311 privatePathPanel.setSpacing(8);
312 privatePathPanel.addStyleName("pithos-TabPanelBottom");
314 privatePathText.setWidth(Const.PERCENT_100);
315 privatePathText.addClickHandler(new ClickHandler() {
317 public void onClick(ClickEvent event) {
318 Pithos.enableIESelection();
319 ((TextBox) event.getSource()).selectAll();
320 Pithos.preventIESelection();
323 privatePathText.setText(Window.Location.getHost() + file.getPublicUri());
324 privatePathText.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
325 privatePathText.setWidth(Const.PERCENT_100);
326 privatePathText.setReadOnly(true);
327 privatePathPanel.add(privatePathText);
329 Scheduler.get().scheduleDeferred(new Command() {
331 public void execute() {
332 showLinkForPrivateSharing();
336 privateSharingPanel.add(privateCheckPanel);
337 privateSharingPanel.add(privatePermPanel);
338 privateSharingPanel.add(privatePathPanel);
341 private Panel createMainPanel() {
342 VerticalPanel panelAll = new VerticalPanel();
343 VerticalPanel panelPublic = new VerticalPanel();
344 VerticalPanel panelPrivate = new VerticalPanel();
346 populatePrivateSharingPanel(panelPrivate);
347 populatePublicSharingPanel(panelPublic);
349 panelAll.add(panelPrivate);
350 panelAll.add(panelPublic);
355 private boolean isFilePubliclyShared() {
356 return file.isPublished();
359 private boolean isFilePrivatelyShared() {
360 return file.isShared();
363 private void showLinkForPublicSharing() {
364 if (isFilePubliclyShared()) {
365 UrlBuilder b = Window.Location.createUrlBuilder();
366 b.setPath(file.getPublicUri());
367 publicPathText.setText(b.buildString());
368 publicPathPanel.setVisible(true);
371 publicPathPanel.setVisible(false);
375 private void showLinkForPrivateSharing() {
376 if (isFilePrivatelyShared()) {
377 UrlBuilder b = Window.Location.createUrlBuilder();
378 b.setPath(app.getApiPath() + file.getOwnerID() + file.getUri());
379 String href = Window.Location.getHref();
380 boolean hasParameters = href.contains(Const.QUESTION_MARK);
381 privatePathText.setText(href + (hasParameters ? Const.AMPERSAND : Const.QUESTION_MARK) + Const.GOTO_EQ + b.buildString());
382 privatePathPanel.setVisible(true);
385 privatePathPanel.setVisible(false);
389 protected void updateMetaDataForPublicSharing(String api, String owner, final String path, final Boolean published) {
390 if (published != null) {
391 PostRequest updateFile = new PostRequest(api, owner, path) {
393 public void onSuccess(Resource result) {
394 HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwnerID(), path, file) {
397 public void onSuccess(File _result) {
398 showLinkForPublicSharing();
399 if (!app.isMySharedSelected()) {
400 app.updateFolder(file.getParent(), true, new Command() {
403 public void execute() {
404 app.updateMySharedRoot();
409 app.updateSharedFolder(file.getParent(), true);
414 public void onError(Throwable t) {
416 app.displayError("System error modifying file:" + t.getMessage());
420 protected void onUnauthorized(Response response) {
421 app.sessionExpired();
424 headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
425 Scheduler.get().scheduleDeferred(headFile);
429 public void onError(Throwable t) {
431 app.displayError("System error modifying file:" + t.getMessage());
435 protected void onUnauthorized(Response response) {
436 app.sessionExpired();
439 updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
440 updateFile.setHeader(Const.X_OBJECT_PUBLIC, published.toString());
441 Scheduler.get().scheduleDeferred(updateFile);
443 else if (!app.isMySharedSelected())
444 app.updateFolder(file.getParent(), true, new Command() {
446 public void execute() {
447 if (file.isSharedOrPublished()) {
448 app.updateMySharedRoot();
453 app.updateSharedFolder(file.getParent(), true);
455 protected void updateMetaDataForPublicSharing(Boolean published) {
456 updateMetaDataForPublicSharing(
459 file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
464 protected void updateMetaDataForPrivateSharing(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) {
465 if (newPermissions != null) {
466 PostRequest updateFile = new PostRequest(api, owner, path) {
468 public void onSuccess(Resource result) {
469 HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwnerID(), path, file) {
472 public void onSuccess(File _result) {
473 showLinkForPrivateSharing();
474 if (!app.isMySharedSelected())
475 app.updateFolder(file.getParent(), true, new Command() {
478 public void execute() {
479 app.updateMySharedRoot();
483 app.updateSharedFolder(file.getParent(), true);
487 public void onError(Throwable t) {
489 app.displayError("System error modifying file:" + t.getMessage());
493 protected void onUnauthorized(Response response) {
494 app.sessionExpired();
497 headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
498 Scheduler.get().scheduleDeferred(headFile);
502 public void onError(Throwable t) {
504 app.displayError("System error modifying file:" + t.getMessage());
508 protected void onUnauthorized(Response response) {
509 app.sessionExpired();
512 updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
514 String readPermHeader = Const.READ_EQ;
515 String writePermHeader = Const.WRITE_EQ;
516 for (String u : newPermissions.keySet()) {
517 Boolean[] p = newPermissions.get(u);
518 if (p[0] != null && p[0]) {
519 readPermHeader += u + Const.COMMA;
521 if (p[1] != null && p[1]) {
522 writePermHeader += u + Const.COMMA;
525 if (readPermHeader.endsWith(Const.EQ)) {
528 else if (readPermHeader.endsWith(Const.COMMA)) {
529 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
531 if (writePermHeader.endsWith(Const.EQ)) {
532 writePermHeader = "";
534 else if (writePermHeader.endsWith(Const.COMMA)) {
535 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
537 String permHeader = readPermHeader +
538 ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? Const.SEMI : "") + writePermHeader;
539 if (permHeader.length() == 0) {
540 permHeader=Const.TILDE;
543 permHeader = URL.encodePathSegment(permHeader);
545 updateFile.setHeader(Const.X_OBJECT_SHARING, permHeader);
546 Scheduler.get().scheduleDeferred(updateFile);
548 else if (!app.isMySharedSelected()) {
549 app.updateFolder(file.getParent(), true, new Command() {
551 public void execute() {
552 if (file.isSharedOrPublished())
553 app.updateMySharedRoot();
558 app.updateSharedFolder(file.getParent(), true);
561 protected void updateMetaDataForPrivateSharing() {
562 updateMetaDataForPrivateSharing(
565 file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
566 permList.getPermissions()
570 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
571 super.onPreviewNativeEvent(preview);
573 NativeEvent evt = preview.getNativeEvent();
574 if (evt.getType().equals(Const.EVENT_TYPE_KEYDOWN) &&
575 evt.getKeyCode() == KeyCodes.KEY_ENTER) {
582 protected boolean accept() {