2 * Copyright 2011 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.FileVersions;
\r
39 import gr.grnet.pithos.web.client.foldertree.Resource;
\r
40 import gr.grnet.pithos.web.client.foldertree.Version;
\r
41 import gr.grnet.pithos.web.client.rest.GetRequest;
\r
42 import gr.grnet.pithos.web.client.rest.PostRequest;
\r
43 import gr.grnet.pithos.web.client.rest.PutRequest;
\r
44 import gr.grnet.pithos.web.client.rest.RestException;
\r
45 import gr.grnet.pithos.web.client.tagtree.Tag;
\r
47 import java.util.List;
\r
48 import java.util.Map;
\r
50 import com.google.gwt.core.client.GWT;
\r
51 import com.google.gwt.core.client.Scheduler;
\r
52 import com.google.gwt.event.dom.client.ClickEvent;
\r
53 import com.google.gwt.event.dom.client.ClickHandler;
\r
54 import com.google.gwt.http.client.Response;
\r
55 import com.google.gwt.http.client.URL;
\r
56 import com.google.gwt.i18n.client.DateTimeFormat;
\r
57 import com.google.gwt.resources.client.ImageResource;
\r
58 import com.google.gwt.user.client.Window;
\r
59 import com.google.gwt.user.client.ui.Anchor;
\r
60 import com.google.gwt.user.client.ui.Button;
\r
61 import com.google.gwt.user.client.ui.CheckBox;
\r
62 import com.google.gwt.user.client.ui.DecoratedTabPanel;
\r
63 import com.google.gwt.user.client.ui.DisclosurePanel;
\r
64 import com.google.gwt.user.client.ui.FlexTable;
\r
65 import com.google.gwt.user.client.ui.FlowPanel;
\r
66 import com.google.gwt.user.client.ui.FocusPanel;
\r
67 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
68 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
69 import com.google.gwt.user.client.ui.Label;
\r
70 import com.google.gwt.user.client.ui.TextBox;
\r
71 import com.google.gwt.user.client.ui.VerticalPanel;
\r
74 * The 'File properties' dialog box implementation.
\r
77 public class FilePropertiesDialog extends AbstractPropertiesDialog {
\r
79 protected PermissionsList permList;
\r
81 protected CheckBox readForAll;
\r
84 * An image bundle for this widgets images.
\r
86 public interface Images extends MessagePanel.Images {
\r
88 @Source("gr/grnet/pithos/resources/edit_user.png")
\r
89 ImageResource permUser();
\r
91 @Source("gr/grnet/pithos/resources/groups22.png")
\r
92 ImageResource permGroup();
\r
94 @Source("gr/grnet/pithos/resources/editdelete.png")
\r
95 ImageResource delete();
\r
97 @Source("gr/grnet/pithos/resources/db_update.png")
\r
98 ImageResource restore();
\r
100 @Source("gr/grnet/pithos/resources/folder_inbox.png")
\r
101 ImageResource download();
\r
105 * The widget that holds the name of the file.
\r
107 private TextBox name = new TextBox();
\r
111 Images images = GWT.create(Images.class);
\r
114 * The widget's constructor.
\r
116 public FilePropertiesDialog(Pithos _app, File _file) {
\r
120 Anchor close = new Anchor();
\r
121 close.addStyleName("close");
\r
122 close.addClickHandler(new ClickHandler() {
\r
125 public void onClick(ClickEvent event) {
\r
129 // Set the dialog's caption.
\r
130 setText("File properties");
\r
131 setAnimationEnabled(true);
\r
132 setGlassEnabled(true);
\r
133 setStyleName("pithos-DialogBox");
\r
135 // Outer contains inner and buttons.
\r
136 final VerticalPanel outer = new VerticalPanel();
\r
138 final FocusPanel focusPanel = new FocusPanel(outer);
\r
139 // Inner contains generalPanel and permPanel.
\r
140 inner = new DecoratedTabPanel();
\r
141 inner.setAnimationEnabled(true);
\r
142 inner.addStyleName("inner");
\r
143 inner.getDeckPanel().addStyleName("pithos-TabPanelBottom");
\r
146 inner.add(createGeneralPanel(), "General");
\r
148 inner.add(createSharingPanel(), "Sharing");
\r
152 inner.selectTab(0);
\r
156 // Create the 'OK' button, along with a listener that hides the dialog
\r
157 // when the button is clicked.
\r
158 final Button ok = new Button("OK", new ClickHandler() {
\r
160 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
165 ok.addStyleName("button");
\r
168 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
\r
170 focusPanel.setFocus(true);
\r
174 protected void fetchVersions() {
\r
175 String path = file.getUri() + "?format=json&version=list";
\r
176 GetRequest<FileVersions> getVersions = new GetRequest<FileVersions>(FileVersions.class, app.getApiPath(), file.getOwner(), path) {
\r
179 public void onSuccess(FileVersions _result) {
\r
180 inner.add(createVersionPanel(_result.getVersions()), "Versions");
\r
184 public void onError(Throwable t) {
\r
186 if (t instanceof RestException) {
\r
187 app.displayError("Unable to fetch versions: " + ((RestException) t).getHttpStatusText());
\r
190 app.displayError("System error unable to fetch versions: "+t.getMessage());
\r
194 protected void onUnauthorized(Response response) {
\r
195 app.sessionExpired();
\r
198 getVersions.setHeader("X-Auth-Token", app.getToken());
\r
199 Scheduler.get().scheduleDeferred(getVersions);
\r
202 private VerticalPanel createGeneralPanel() {
\r
203 final VerticalPanel generalPanel = new VerticalPanel();
\r
204 final FlexTable generalTable = new FlexTable();
\r
205 generalTable.setText(0, 0, "Name");
\r
206 generalTable.setText(1, 0, "Folder");
\r
207 generalTable.setText(2, 0, "Owner");
\r
208 generalTable.setText(3, 0, "Last modified");
\r
209 generalTable.setText(4, 0, "Tags");
\r
211 name.setWidth("100%");
\r
212 name.setText(file.getName());
\r
213 generalTable.setWidget(0, 1, name);
\r
214 if(file.getParent() != null)
\r
215 generalTable.setText(1, 1, file.getParent().getName());
\r
217 generalTable.setText(1, 1, "-");
\r
218 generalTable.setText(2, 1, file.getOwner());
\r
220 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
221 generalTable.setText(3, 1, file.getLastModified() != null ? formatter.format(file.getLastModified()) : "");
\r
223 StringBuffer tagsBuffer = new StringBuffer();
\r
224 for (String t : file.getTags())
\r
225 tagsBuffer.append(t).append(", ");
\r
226 if (tagsBuffer.length() > 1)
\r
227 tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
\r
228 initialTagText = tagsBuffer.toString();
\r
229 tags.setWidth("100%");
\r
230 tags.setText(initialTagText);
\r
231 generalTable.setWidget(4, 1, tags);
\r
233 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
234 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
235 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
236 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
237 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
\r
238 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
239 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
240 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
241 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
242 generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
\r
243 generalTable.setCellSpacing(4);
\r
245 generalPanel.add(generalTable);
\r
247 DisclosurePanel allTags = new DisclosurePanel("All tags");
\r
248 allTagsContent = new FlowPanel();
\r
249 allTagsContent.setWidth("100%");
\r
250 for (Tag t : app.getAllTags()) {
\r
251 final Anchor tagAnchor = new Anchor(t.getName(), false);
\r
252 tagAnchor.addStyleName("pithos-tag");
\r
253 allTagsContent.add(tagAnchor);
\r
254 Label separator = new Label(", ");
\r
255 separator.addStyleName("pithos-tag");
\r
256 allTagsContent.add(separator);
\r
257 tagAnchor.addClickHandler(new ClickHandler() {
\r
260 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
261 String existing = tags.getText().trim();
\r
262 if (MULTIPLE_VALUES_TEXT.equals(existing))
\r
264 String newTag = tagAnchor.getText().trim();
\r
265 // insert the new tag only if it is not in the list
\r
267 if (existing.indexOf(newTag) == -1)
\r
268 tags.setText(existing + (existing.length() > 0 ? ", " : "") + newTag);
\r
272 allTags.setContent(allTagsContent);
\r
273 generalPanel.add(allTags);
\r
274 generalPanel.setSpacing(4);
\r
275 return generalPanel;
\r
278 private VerticalPanel createSharingPanel() {
\r
279 VerticalPanel permPanel = new VerticalPanel();
\r
281 permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);
\r
282 permPanel.add(permList);
\r
284 if (file.getInheritedPermissionsFrom() == null) {
\r
285 HorizontalPanel permButtons = new HorizontalPanel();
\r
286 Button add = new Button("Add Group", new ClickHandler() {
\r
288 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
289 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
\r
291 permList.updatePermissionTable();
\r
294 add.addStyleName("button");
\r
295 permButtons.add(add);
\r
296 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
298 final Button addUser = new Button("Add User", new ClickHandler() {
\r
300 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
301 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
\r
303 permList.updatePermissionTable();
\r
306 addUser.addStyleName("button");
\r
307 permButtons.add(addUser);
\r
308 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
310 permButtons.setSpacing(8);
\r
311 permButtons.addStyleName("pithos-TabPanelBottom");
\r
312 permPanel.add(permButtons);
\r
315 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
\r
316 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
317 "distribute this file and that it does not violate the Terms of Use.", true);
\r
318 readForAllNote.setVisible(false);
\r
319 readForAllNote.setStylePrimaryName("pithos-readForAllNote");
\r
321 readForAll = new CheckBox();
\r
322 readForAll.setValue(file.isPublished());
\r
323 readForAll.addClickHandler(new ClickHandler() {
\r
325 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
326 readForAllNote.setVisible(readForAll.getValue());
\r
330 // Only show the read for all permission if the user is the owner.
\r
331 if (file.getOwner().equals(app.getUsername())) {
\r
332 final HorizontalPanel permForAll = new HorizontalPanel();
\r
333 permForAll.add(new Label("Public"));
\r
334 permForAll.add(readForAll);
\r
335 permForAll.setSpacing(8);
\r
336 permForAll.addStyleName("pithos-TabPanelBottom");
\r
337 permForAll.add(readForAllNote);
\r
338 permPanel.add(permForAll);
\r
341 if (file.isPublished()) {
\r
342 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
343 pathPanel.setWidth("100%");
\r
344 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
345 pathPanel.add(new Label("Link"));
\r
346 pathPanel.setSpacing(8);
\r
347 pathPanel.addStyleName("pithos-TabPanelBottom");
\r
349 TextBox path = new TextBox();
\r
350 path.setWidth("100%");
\r
351 path.addClickHandler(new ClickHandler() {
\r
353 public void onClick(ClickEvent event) {
\r
354 Pithos.enableIESelection();
\r
355 ((TextBox) event.getSource()).selectAll();
\r
356 Pithos.preventIESelection();
\r
359 path.setText(Window.Location.getHost() + file.getPublicUri());
\r
360 path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
361 path.setWidth("100%");
\r
362 path.setReadOnly(true);
\r
363 pathPanel.add(path);
\r
364 permPanel.add(pathPanel);
\r
370 VerticalPanel createVersionPanel(List<Version> versions) {
\r
371 VerticalPanel versionPanel = new VerticalPanel();
\r
372 VersionsList verList = new VersionsList(app, this, images, file, versions);
\r
373 versionPanel.add(verList);
\r
374 return versionPanel;
\r
378 * Accepts any change and updates the file
\r
382 protected void accept() {
\r
383 String newFilename = null;
\r
385 final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
\r
387 if (!name.getText().trim().equals(file.getName())) {
\r
388 newFilename = name.getText().trim();
\r
391 //only update the read for all perm if the user is the owner
\r
392 Boolean published = null;
\r
393 if (readForAll.getValue() != file.isPublished())
\r
394 if (file.getOwner().equals(app.getUsername()))
\r
395 published = readForAll.getValue();
\r
396 final Boolean finalPublished = published;
\r
398 String[] tagset = null;
\r
399 if (!tags.getText().equals(initialTagText))
\r
400 tagset = tags.getText().trim().split(",");
\r
401 final String[] newTags = tagset;
\r
403 if (newFilename != null) {
\r
404 final String path = file.getParent().getUri() + "/" + newFilename;
\r
405 PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
\r
407 public void onSuccess(@SuppressWarnings("unused") Resource result) {
\r
408 updateMetaData(app.getApiPath(), file.getOwner(), path + "?update=", newTags, finalPublished, perms);
\r
412 public void onError(Throwable t) {
\r
414 app.displayError("System error modifying file:" + t.getMessage());
\r
418 protected void onUnauthorized(Response response) {
\r
419 app.sessionExpired();
\r
422 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
423 updateFile.setHeader("X-Move-From", file.getUri());
\r
424 updateFile.setHeader("Content-Type", file.getContentType());
\r
425 Scheduler.get().scheduleDeferred(updateFile);
\r
428 updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished, perms);
\r
431 protected void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published, Map<String, Boolean[]> newPermissions) {
\r
432 if (newTags != null || published != null || newPermissions != null) {
\r
433 PostRequest updateFile = new PostRequest(api, owner, path) {
\r
435 public void onSuccess(@SuppressWarnings("unused") Resource result) {
\r
436 app.updateFolder(file.getParent(), true, null);
\r
440 public void onError(Throwable t) {
\r
442 app.displayError("System error modifying file:" + t.getMessage());
\r
446 protected void onUnauthorized(Response response) {
\r
447 app.sessionExpired();
\r
450 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
451 for (String t : file.getTags()) {
\r
452 updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(t.trim()), "~");
\r
454 if (newTags != null)
\r
455 for (String t : newTags)
\r
456 if (t.length() > 0)
\r
457 updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(t.trim()), "true");
\r
458 if (published != null)
\r
459 updateFile.setHeader("X-Object-Public", published.toString());
\r
460 if (newPermissions != null) {
\r
461 String readPermHeader = "read=";
\r
462 String writePermHeader = "write=";
\r
463 for (String u : newPermissions.keySet()) {
\r
464 Boolean[] p = newPermissions.get(u);
\r
465 if (p[0] != null && p[0])
\r
466 readPermHeader += u + ",";
\r
467 if (p[1] != null && p[1])
\r
468 writePermHeader += u + ",";
\r
470 if (readPermHeader.endsWith("="))
\r
471 readPermHeader = "";
\r
472 else if (readPermHeader.endsWith(","))
\r
473 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
\r
474 if (writePermHeader.endsWith("="))
\r
475 writePermHeader = "";
\r
476 else if (writePermHeader.endsWith(","))
\r
477 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
\r
478 String permHeader = readPermHeader + ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? ";" : "") + writePermHeader;
\r
479 if (permHeader.length() == 0)
\r
481 updateFile.setHeader("X-Object-Sharing", permHeader);
\r
483 Scheduler.get().scheduleDeferred(updateFile);
\r
486 app.updateFolder(file.getParent(), true, null);
\r