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);
276 Button addGroup = new Button("Add Group", new ClickHandler() {
278 public void onClick(ClickEvent event) {
279 if (app.getAccount().getGroups().isEmpty()) {
280 new GroupCreateDialog(app, new Command() {
283 public void execute() {
284 if (app.getAccount().getGroups().isEmpty()) {
287 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
289 permList.updatePermissionTable();
294 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
296 permList.updatePermissionTable();
300 addGroup.addStyleName("button");
301 permButtons.add(addGroup);
304 Button addEverybody = new Button("Add everybody", new ClickHandler() {
306 public void onClick(ClickEvent event) {
307 Pithos.LOG("Adding to Everybody");
309 permList.addPermission("*", true, false);
310 permList.updatePermissionTable();
313 addEverybody.addStyleName("button");
314 permButtons.add(addEverybody);
316 permButtons.setCellHorizontalAlignment(addGroup, HasHorizontalAlignment.ALIGN_CENTER);
317 privatePermPanel.add(permList);
318 privatePermPanel.add(permButtons);
321 privatePathPanel.setVisible(false);
322 privatePathPanel.setWidth(Const.PERCENT_100);
323 privatePathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
324 privatePathPanel.add(new Label("Link"));
325 privatePathPanel.setSpacing(8);
326 privatePathPanel.addStyleName("pithos-TabPanelBottom");
328 privatePathText.setWidth(Const.PERCENT_100);
329 privatePathText.addClickHandler(new ClickHandler() {
331 public void onClick(ClickEvent event) {
332 Pithos.enableIESelection();
333 ((TextBox) event.getSource()).selectAll();
334 Pithos.preventIESelection();
337 privatePathText.setText(Window.Location.getHost() + file.getPublicUri());
338 privatePathText.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
339 privatePathText.setWidth(Const.PERCENT_100);
340 privatePathText.setReadOnly(true);
341 privatePathPanel.add(privatePathText);
343 Scheduler.get().scheduleDeferred(new Command() {
345 public void execute() {
346 showLinkForPrivateSharing();
350 privateSharingPanel.add(privateCheckPanel);
351 privateSharingPanel.add(privatePermPanel);
352 privateSharingPanel.add(privatePathPanel);
355 private Panel createMainPanel() {
356 VerticalPanel panelAll = new VerticalPanel();
357 VerticalPanel panelPublic = new VerticalPanel();
358 VerticalPanel panelPrivate = new VerticalPanel();
360 populatePrivateSharingPanel(panelPrivate);
361 populatePublicSharingPanel(panelPublic);
363 panelAll.add(panelPrivate);
364 panelAll.add(panelPublic);
369 private boolean isFilePubliclyShared() {
370 return file.isPublished();
373 private boolean isFilePrivatelyShared() {
374 return file.isShared();
377 private void showLinkForPublicSharing() {
378 if (isFilePubliclyShared()) {
379 UrlBuilder b = Window.Location.createUrlBuilder();
380 b.setPath(file.getPublicUri());
381 publicPathText.setText(b.buildString());
382 publicPathPanel.setVisible(true);
385 publicPathPanel.setVisible(false);
389 private void showLinkForPrivateSharing() {
390 if (isFilePrivatelyShared()) {
391 UrlBuilder b = Window.Location.createUrlBuilder();
392 b.setPath(app.getApiPath() + file.getOwnerID() + file.getUri());
393 String href = Window.Location.getHref();
394 boolean hasParameters = href.contains(Const.QUESTION_MARK);
395 privatePathText.setText(href + (hasParameters ? Const.AMPERSAND : Const.QUESTION_MARK) + Const.GOTO_EQ + b.buildString());
396 privatePathPanel.setVisible(true);
399 privatePathPanel.setVisible(false);
403 protected void updateMetaDataForPublicSharing(String api, String owner, final String path, final Boolean published) {
404 if (published != null) {
405 PostRequest updateFile = new PostRequest(api, owner, path) {
407 public void onSuccess(Resource result) {
408 HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwnerID(), path, file) {
411 public void onSuccess(File _result) {
412 showLinkForPublicSharing();
413 if (!app.isMySharedSelected()) {
414 app.updateFolder(file.getParent(), true, new Command() {
417 public void execute() {
418 app.updateMySharedRoot();
423 app.updateSharedFolder(file.getParent(), true);
428 public void onError(Throwable t) {
430 app.displayError("System error modifying file:" + t.getMessage());
434 protected void onUnauthorized(Response response) {
435 app.sessionExpired();
438 headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
439 Scheduler.get().scheduleDeferred(headFile);
443 public void onError(Throwable t) {
445 app.displayError("System error modifying file:" + t.getMessage());
449 protected void onUnauthorized(Response response) {
450 app.sessionExpired();
453 updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
454 updateFile.setHeader(Const.X_OBJECT_PUBLIC, published.toString());
455 Scheduler.get().scheduleDeferred(updateFile);
457 else if (!app.isMySharedSelected())
458 app.updateFolder(file.getParent(), true, new Command() {
460 public void execute() {
461 if (file.isSharedOrPublished()) {
462 app.updateMySharedRoot();
467 app.updateSharedFolder(file.getParent(), true);
469 protected void updateMetaDataForPublicSharing(Boolean published) {
470 updateMetaDataForPublicSharing(
473 file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
478 protected void updateMetaDataForPrivateSharing(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) {
479 if (newPermissions != null) {
480 PostRequest updateFile = new PostRequest(api, owner, path) {
482 public void onSuccess(Resource result) {
483 HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwnerID(), path, file) {
486 public void onSuccess(File _result) {
487 showLinkForPrivateSharing();
488 if (!app.isMySharedSelected())
489 app.updateFolder(file.getParent(), true, new Command() {
492 public void execute() {
493 app.updateMySharedRoot();
497 app.updateSharedFolder(file.getParent(), true);
501 public void onError(Throwable t) {
503 app.displayError("System error modifying file:" + t.getMessage());
507 protected void onUnauthorized(Response response) {
508 app.sessionExpired();
511 headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
512 Scheduler.get().scheduleDeferred(headFile);
516 public void onError(Throwable t) {
518 app.displayError("System error modifying file:" + t.getMessage());
522 protected void onUnauthorized(Response response) {
523 app.sessionExpired();
526 updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
528 String readPermHeader = Const.READ_EQ;
529 String writePermHeader = Const.WRITE_EQ;
530 for (String u : newPermissions.keySet()) {
531 Boolean[] p = newPermissions.get(u);
532 if (p[0] != null && p[0]) {
533 readPermHeader += u + Const.COMMA;
535 if (p[1] != null && p[1]) {
536 writePermHeader += u + Const.COMMA;
539 if (readPermHeader.endsWith(Const.EQ)) {
542 else if (readPermHeader.endsWith(Const.COMMA)) {
543 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
545 if (writePermHeader.endsWith(Const.EQ)) {
546 writePermHeader = "";
548 else if (writePermHeader.endsWith(Const.COMMA)) {
549 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
551 String permHeader = readPermHeader +
552 ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? Const.SEMI : "") + writePermHeader;
553 if (permHeader.length() == 0) {
554 permHeader=Const.TILDE;
557 permHeader = URL.encodePathSegment(permHeader);
559 updateFile.setHeader(Const.X_OBJECT_SHARING, permHeader);
560 Scheduler.get().scheduleDeferred(updateFile);
562 else if (!app.isMySharedSelected()) {
563 app.updateFolder(file.getParent(), true, new Command() {
565 public void execute() {
566 if (file.isSharedOrPublished())
567 app.updateMySharedRoot();
572 app.updateSharedFolder(file.getParent(), true);
575 protected void updateMetaDataForPrivateSharing() {
576 updateMetaDataForPrivateSharing(
579 file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
580 permList.getPermissions()
584 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
585 super.onPreviewNativeEvent(preview);
587 NativeEvent evt = preview.getNativeEvent();
588 if (evt.getType().equals(Const.EVENT_TYPE_KEYDOWN) &&
589 evt.getKeyCode() == KeyCodes.KEY_ENTER) {
596 protected boolean accept() {