Remove unused interface
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileShareDialog.java
1 /*
2  * Copyright 2011-2013 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
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.
16  *
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.
29  *
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.
34  */
35 package gr.grnet.pithos.web.client;
36
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;
42
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;
55
56 import java.util.Map;
57
58 /**
59  * UI for the "Share" command.
60  */
61 public class FileShareDialog extends AbstractPropertiesDialog {
62     private static final class PermissionsUncheckWarning extends AbstractPropertiesDialog {
63         private PermissionsUncheckWarning(Pithos app) {
64             super(app);
65             final Anchor close = new Anchor("close");
66             close.addStyleName("close");
67             close.addClickHandler(new ClickHandler() {
68                 @Override
69                 public void onClick(ClickEvent event) {
70                     hide();
71                 }
72             });
73             final String dialogText = "Info";
74             setText(dialogText);
75             setStyleName("pithos-DialogBox");
76
77             final VerticalPanel panel = new VerticalPanel();
78             panel.add(close);
79
80             VerticalPanel inner = new VerticalPanel();
81             inner.addStyleName("inner");
82
83             inner.add(
84                 new InlineHTML(
85                     "It seems you are already sharing this file with some users." +
86                     "<br>" +
87                     "Please remove all users from the sharing list, to be able to uncheck this option."
88                 )
89             );
90
91             final Button ok = new Button("OK", new ClickHandler() {
92                 @Override
93                 public void onClick(ClickEvent event) {
94                     hide();
95                 }
96             });
97
98             ok.addStyleName("button");
99             inner.add(ok);
100
101             panel.add(inner);
102             panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
103
104             setWidget(panel);
105         }
106
107         @Override
108         protected boolean accept() {
109             return true;
110         }
111     }
112
113     // For public sharing
114         private final HorizontalPanel publicPathPanel = new HorizontalPanel();
115         private final TextBox publicPathText = new TextBox();
116
117     // For private sharing
118     private final HorizontalPanel privatePathPanel = new HorizontalPanel();
119     private final TextBox privatePathText = new TextBox();
120     private PermissionsList permList;
121         
122     public interface PrivateSharingImages extends MessagePanel.Images {
123
124         @Source("gr/grnet/pithos/resources/edit_user.png")
125         ImageResource permUser();
126
127         @Source("gr/grnet/pithos/resources/groups22.png")
128         ImageResource permGroup();
129
130         @Source("gr/grnet/pithos/resources/delete.gif")
131         ImageResource delete();
132     }
133
134     private final File file;
135
136     private final PrivateSharingImages privateSharingImages = GWT.create(PrivateSharingImages.class);
137
138         /**
139          * The widget's constructor.
140          */
141         public FileShareDialog(Pithos app, File file) {
142         super(app);
143         this.file = file;
144
145                 Anchor close = new Anchor("close");
146                 close.addStyleName("close");
147                 close.addClickHandler(new ClickHandler() {
148                         
149                         @Override
150                         public void onClick(ClickEvent event) {
151                                 hide();
152                         }
153                 });
154                 // Set the dialog's caption.
155                 setText(Const.TXT_SHARE_FILE);
156                 setGlassEnabled(true);
157                 setStyleName("pithos-DialogBox");
158
159                 // Outer contains inner and buttons.
160                 final VerticalPanel outer = new VerticalPanel();
161                 outer.add(close);
162                 final FocusPanel focusPanel = new FocusPanel(outer);
163                 // Inner contains generalPanel and permPanel.
164                 inner = new VerticalPanel();
165                 inner.addStyleName("inner");
166
167         inner.add(createMainPanel());
168
169         outer.add(inner);
170
171                 final Button ok = new Button("OK", new ClickHandler() {
172                         @Override
173                         public void onClick(ClickEvent event) {
174                                 closeDialog();
175                         }
176                 });
177                 ok.addStyleName("button");
178
179         outer.add(ok);
180         outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
181
182         focusPanel.setFocus(true);
183         setWidget(outer);
184         }
185
186     private boolean IamOwner() {
187         return file.getOwnerID().equals(app.getUserID());
188     }
189
190     private void populatePublicSharingPanel(VerticalPanel publicSharingPanel) {
191         if(IamOwner()) {
192             final HorizontalPanel publicCheckPanel = new HorizontalPanel();
193             publicCheckPanel.setSpacing(8);
194
195             // Check box header
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);
199
200             publicCheckBox.setValue(isFilePubliclyShared());
201             publicCheckBox.addClickHandler(new ClickHandler() {
202                 @Override
203                 public void onClick(ClickEvent event) {
204                     final Boolean published;
205                     if(publicCheckBox.getValue() != file.isPublished() && IamOwner()) {
206                         published = publicCheckBox.getValue();
207                     }
208                     else {
209                         published = Boolean.FALSE;
210                     }
211
212                     updateMetaDataForPublicSharing(published);
213                 }
214             });
215
216             publicCheckPanel.add(publicCheckBox);
217             publicCheckPanel.add(publicCheckTitle);
218             publicCheckPanel.add(publicCheckInfo);
219
220             // Public Link
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");
227
228             publicPathText.setWidth(Const.PERCENT_100);
229             publicPathText.addClickHandler(new ClickHandler() {
230                 @Override
231                 public void onClick(ClickEvent event) {
232                     Pithos.enableIESelection();
233                     ((TextBox) event.getSource()).selectAll();
234                     Pithos.preventIESelection();
235                 }
236             });
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);
241
242             publicSharingPanel.add(publicCheckPanel);
243             publicSharingPanel.add(publicPathPanel);
244
245             Scheduler.get().scheduleDeferred(new Command() {
246                 @Override
247                 public void execute() {
248                     showLinkForPublicSharing();
249                 }
250             });
251         }
252     }
253
254     private void populatePrivateSharingPanel(VerticalPanel privateSharingPanel) {
255         final HorizontalPanel privateCheckPanel = new HorizontalPanel();
256         final VerticalPanel privatePermPanel = new VerticalPanel();
257         final HorizontalPanel permButtons = new HorizontalPanel();
258
259         privateCheckPanel.setSpacing(8);
260         privatePermPanel.setVisible(isFilePrivatelyShared());
261         permButtons.setSpacing(8);
262
263         // Check box header
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);
267
268         privateCheckBox.setValue(isFilePrivatelyShared());
269         privateCheckBox.addClickHandler(new ClickHandler() {
270             @Override
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;
275
276                 if(isFilePrivatelyShared()) {
277                     // ignore the click, set it always to "checked"
278                     privateCheckBox.setValue(true);
279                     // show permissions
280                     privatePermPanel.setVisible(true);
281
282                     // Refs #3593
283                     if(userUncheckedIt) {
284                         new PermissionsUncheckWarning(app).center();
285                     }
286                 }
287                 else {
288                     privatePermPanel.setVisible(userCheckedIt);
289                 }
290             }
291         });
292
293         privateCheckPanel.add(privateCheckBox);
294         privateCheckPanel.add(privateCheckTitle);
295         privateCheckPanel.add(privateCheckInfo);
296
297         // Permission list
298         permList = new PermissionsList(app, privateSharingImages, file.getPermissions(), file.getOwnerID(), false, new Command() {
299             @Override
300             public void execute() {
301                 updateMetaDataForPrivateSharing();
302             }
303         });
304
305         // Permission Add buttons
306         Button addUser = new Button("Add User", new ClickHandler() {
307             @Override
308             public void onClick(ClickEvent event) {
309                 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
310                 dlg.center();
311                 permList.updatePermissionTable();
312             }
313         });
314         addUser.addStyleName("button");
315         permButtons.add(addUser);
316
317         final boolean haveGroups = app.getAccount().getGroups().size() > 0;
318
319         if(haveGroups) {
320             Button addGroup = new Button("Add Group", new ClickHandler() {
321                 @Override
322                 public void onClick(ClickEvent event) {
323                     PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
324                     dlg.center();
325                     permList.updatePermissionTable();
326                 }
327             });
328             addGroup.addStyleName("button");
329
330             permButtons.add(addGroup);
331             permButtons.setCellHorizontalAlignment(addGroup, HasHorizontalAlignment.ALIGN_CENTER);
332         }
333
334         final Button addEverybody = new Button("Add everybody", new ClickHandler() {
335             @Override
336             public void onClick(ClickEvent event) {
337                 Pithos.LOG("Adding to Everybody");
338                 Pithos.LOG("");
339                 permList.addPermission("*", true, false);
340                 permList.updatePermissionTable();
341             }
342         });
343         addEverybody.addStyleName("button");
344         permButtons.add(addEverybody);
345
346         privatePermPanel.add(permList);
347         privatePermPanel.add(permButtons);
348
349         // Private Link
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");
356
357         privatePathText.setWidth(Const.PERCENT_100);
358         privatePathText.addClickHandler(new ClickHandler() {
359             @Override
360             public void onClick(ClickEvent event) {
361                 Pithos.enableIESelection();
362                 ((TextBox) event.getSource()).selectAll();
363                 Pithos.preventIESelection();
364             }
365         });
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);
371
372         Scheduler.get().scheduleDeferred(new Command() {
373             @Override
374             public void execute() {
375                 showLinkForPrivateSharing();
376             }
377         });
378
379         privateSharingPanel.add(privateCheckPanel);
380         privateSharingPanel.add(privatePermPanel);
381         privateSharingPanel.add(privatePathPanel);
382     }
383
384     private Panel createMainPanel() {
385         VerticalPanel panelAll = new VerticalPanel();
386         VerticalPanel panelPublic = new VerticalPanel();
387         VerticalPanel panelPrivate = new VerticalPanel();
388
389         populatePrivateSharingPanel(panelPrivate);
390         populatePublicSharingPanel(panelPublic);
391
392         panelAll.add(panelPrivate);
393         panelAll.add(new InlineHTML("<hr/>"));
394         panelAll.add(panelPublic);
395
396         return panelAll;
397     }
398
399     private boolean isFilePubliclyShared() {
400         return file.isPublished();
401     }
402
403     private boolean isFilePrivatelyShared() {
404         return file.isShared();
405     }
406
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);
413                 }
414                 else {
415                         publicPathPanel.setVisible(false);
416                 }
417     }
418
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);
427         }
428         else {
429             privatePathPanel.setVisible(false);
430         }
431     }
432
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) {
436                 @Override
437                 public void onSuccess(Resource result) {
438                         HeadRequest<File> headFile = new HeadRequest<File>(File.class, Pithos.getStorageAPIURL(), file.getOwnerID(), path, file) {
439
440                                                 @Override
441                                                 public void onSuccess(File _result) {
442                                                         showLinkForPublicSharing();
443                                                         if (!app.isMySharedSelected()) {
444                                             app.updateFolder(file.getParent(), true, new Command() {
445                                                                         
446                                                                         @Override
447                                                                         public void execute() {
448                                                                                 app.updateMySharedRoot();
449                                                                         }
450                                                                 }, true);
451                             }
452                                                         else {
453                                                                 app.updateSharedFolder(file.getParent(), true);
454                             }
455                                                 }
456
457                                                 @Override
458                                                 public void onError(Throwable t) {
459                                                         app.setError(t);
460                                     app.displayError("System error modifying file:" + t.getMessage());
461                                                 }
462
463                                                 @Override
464                                                 protected void onUnauthorized(Response response) {
465                                                         app.sessionExpired();
466                                                 }
467                                         };
468                                         headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
469                                         Scheduler.get().scheduleDeferred(headFile);
470                 }
471
472                 @Override
473                 public void onError(Throwable t) {
474                                         app.setError(t);
475                     app.displayError("System error modifying file:" + t.getMessage());
476                 }
477
478                                 @Override
479                                 protected void onUnauthorized(Response response) {
480                                         app.sessionExpired();
481                                 }
482             };
483             updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
484             updateFile.setHeader(Const.X_OBJECT_PUBLIC, published.toString());
485             Scheduler.get().scheduleDeferred(updateFile);
486         }
487         else if (!app.isMySharedSelected())
488             app.updateFolder(file.getParent(), true, new Command() {
489                                 @Override
490                                 public void execute() {
491                                         if (file.isSharedOrPublished()) {
492                         app.updateMySharedRoot();
493                     }
494                                 }
495                         }, true);
496         else
497                 app.updateSharedFolder(file.getParent(), true);
498     }
499     protected void updateMetaDataForPublicSharing(Boolean published) {
500         updateMetaDataForPublicSharing(
501             Pithos.getStorageAPIURL(),
502             app.getUserID(),
503             file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
504             published
505         );
506     }
507
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) {
511                 @Override
512                 public void onSuccess(Resource result) {
513                     HeadRequest<File> headFile = new HeadRequest<File>(File.class, Pithos.getStorageAPIURL(), file.getOwnerID(), path, file) {
514
515                         @Override
516                         public void onSuccess(File _result) {
517                             showLinkForPrivateSharing();
518                             if (!app.isMySharedSelected())
519                                 app.updateFolder(file.getParent(), true, new Command() {
520
521                                     @Override
522                                     public void execute() {
523                                         app.updateMySharedRoot();
524                                     }
525                                 }, true);
526                             else
527                                 app.updateSharedFolder(file.getParent(), true);
528                         }
529
530                         @Override
531                         public void onError(Throwable t) {
532                             app.setError(t);
533                             app.displayError("System error modifying file:" + t.getMessage());
534                         }
535
536                         @Override
537                         protected void onUnauthorized(Response response) {
538                             app.sessionExpired();
539                         }
540                     };
541                     headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
542                     Scheduler.get().scheduleDeferred(headFile);
543                 }
544
545                 @Override
546                 public void onError(Throwable t) {
547                     app.setError(t);
548                     app.displayError("System error modifying file:" + t.getMessage());
549                 }
550
551                 @Override
552                 protected void onUnauthorized(Response response) {
553                     app.sessionExpired();
554                 }
555             };
556             updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
557
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;
564                 }
565                 if (p[1] != null && p[1]) {
566                     writePermHeader += u + Const.COMMA;
567                 }
568             }
569             if (readPermHeader.endsWith(Const.EQ)) {
570                 readPermHeader = "";
571             }
572             else if (readPermHeader.endsWith(Const.COMMA)) {
573                 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
574             }
575             if (writePermHeader.endsWith(Const.EQ)) {
576                 writePermHeader = "";
577             }
578             else if (writePermHeader.endsWith(Const.COMMA)) {
579                 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
580             }
581             String permHeader = readPermHeader +
582                 ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  Const.SEMI : "") + writePermHeader;
583             if (permHeader.length() == 0) {
584                 permHeader=Const.TILDE;
585             }
586             else {
587                 permHeader = URL.encodePathSegment(permHeader);
588             }
589             updateFile.setHeader(Const.X_OBJECT_SHARING, permHeader);
590             Scheduler.get().scheduleDeferred(updateFile);
591         }
592         else if (!app.isMySharedSelected()) {
593             app.updateFolder(file.getParent(), true, new Command() {
594                 @Override
595                 public void execute() {
596                     if (file.isSharedOrPublished())
597                         app.updateMySharedRoot();
598                 }
599             }, true);
600         }
601         else {
602             app.updateSharedFolder(file.getParent(), true);
603         }
604     }
605     protected void updateMetaDataForPrivateSharing() {
606         updateMetaDataForPrivateSharing(
607             Pithos.getStorageAPIURL(),
608             app.getUserID(),
609             file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
610             permList.getPermissions()
611         );
612     }
613
614     @Override
615         protected void onPreviewNativeEvent(NativePreviewEvent preview) {
616             super.onPreviewNativeEvent(preview);
617
618             NativeEvent evt = preview.getNativeEvent();
619             if (evt.getType().equals(Const.EVENT_TYPE_KEYDOWN) &&
620             evt.getKeyCode() == KeyCodes.KEY_ENTER) {
621
622             closeDialog();
623         }
624         }
625
626     @Override
627     protected boolean accept() {
628         return true;
629     }
630 }