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.event.logical.shared.SelectionEvent;
\r
55 import com.google.gwt.event.logical.shared.SelectionHandler;
\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/groupevent.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 // Set the dialog's caption.
\r
121 setText("File properties");
\r
123 // Outer contains inner and buttons.
\r
124 final VerticalPanel outer = new VerticalPanel();
\r
125 final FocusPanel focusPanel = new FocusPanel(outer);
\r
126 // Inner contains generalPanel and permPanel.
\r
127 inner = new DecoratedTabPanel();
\r
128 inner.setAnimationEnabled(true);
\r
131 inner.add(createGeneralPanel(), "General");
\r
133 inner.add(createSharingPanel(), "Sharing");
\r
137 inner.selectTab(0);
\r
141 final HorizontalPanel buttons = new HorizontalPanel();
\r
142 // Create the 'OK' button, along with a listener that hides the dialog
\r
143 // when the button is clicked.
\r
144 final Button ok = new Button("OK", new ClickHandler() {
\r
146 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
153 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
154 // Create the 'Cancel' button, along with a listener that hides the
\r
155 // dialog when the button is clicked.
\r
156 final Button cancel = new Button("Cancel", new ClickHandler() {
\r
158 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
162 buttons.add(cancel);
\r
163 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
164 buttons.setSpacing(8);
\r
165 buttons.addStyleName("pithos-TabPanelBottom");
\r
167 outer.add(buttons);
\r
168 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
169 outer.addStyleName("pithos-TabPanelBottom");
\r
171 focusPanel.setFocus(true);
\r
175 protected void fetchVersions() {
\r
176 String path = file.getUri() + "?format=json&version=list";
\r
177 GetRequest<FileVersions> getVersions = new GetRequest<FileVersions>(FileVersions.class, app.getApiPath(), file.getOwner(), path) {
\r
180 public void onSuccess(FileVersions _result) {
\r
181 inner.add(createVersionPanel(_result.getVersions()), "Versions");
\r
185 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
193 getVersions.setHeader("X-Auth-Token", app.getToken());
\r
194 Scheduler.get().scheduleDeferred(getVersions);
\r
197 private VerticalPanel createGeneralPanel() {
\r
198 final VerticalPanel generalPanel = new VerticalPanel();
\r
199 final FlexTable generalTable = new FlexTable();
\r
200 generalTable.setText(0, 0, "Name");
\r
201 generalTable.setText(1, 0, "Folder");
\r
202 generalTable.setText(2, 0, "Owner");
\r
203 generalTable.setText(3, 0, "Last modified");
\r
204 generalTable.setText(4, 0, "Tags");
\r
206 name.setWidth("100%");
\r
207 name.setText(file.getName());
\r
208 generalTable.setWidget(0, 1, name);
\r
209 if(file.getParent() != null)
\r
210 generalTable.setText(1, 1, file.getParent().getName());
\r
212 generalTable.setText(1, 1, "-");
\r
213 generalTable.setText(2, 1, file.getOwner());
\r
215 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
216 generalTable.setText(3, 1, formatter.format(file.getLastModified()));
\r
218 StringBuffer tagsBuffer = new StringBuffer();
\r
219 for (String t : file.getTags())
\r
220 tagsBuffer.append(t).append(", ");
\r
221 if (tagsBuffer.length() > 1)
\r
222 tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
\r
223 initialTagText = tagsBuffer.toString();
\r
224 tags.setWidth("100%");
\r
225 tags.setText(initialTagText);
\r
226 generalTable.setWidget(4, 1, tags);
\r
228 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
229 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
230 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
231 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
232 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
\r
233 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
234 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
235 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
236 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
237 generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
\r
238 generalTable.setCellSpacing(4);
\r
240 generalPanel.add(generalTable);
\r
242 DisclosurePanel allTags = new DisclosurePanel("All tags");
\r
243 allTagsContent = new FlowPanel();
\r
244 allTagsContent.setWidth("100%");
\r
245 for (Tag t : app.getAllTags()) {
\r
246 final Anchor tagAnchor = new Anchor(t.getName(), false);
\r
247 tagAnchor.addStyleName("pithos-tag");
\r
248 allTagsContent.add(tagAnchor);
\r
249 Label separator = new Label(", ");
\r
250 separator.addStyleName("pithos-tag");
\r
251 allTagsContent.add(separator);
\r
252 tagAnchor.addClickHandler(new ClickHandler() {
\r
255 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
256 String existing = tags.getText().trim();
\r
257 if (MULTIPLE_VALUES_TEXT.equals(existing))
\r
259 String newTag = tagAnchor.getText().trim();
\r
260 // insert the new tag only if it is not in the list
\r
262 if (existing.indexOf(newTag) == -1)
\r
263 tags.setText(existing + (existing.length() > 0 ? ", " : "") + newTag);
\r
267 allTags.setContent(allTagsContent);
\r
268 generalPanel.add(allTags);
\r
269 generalPanel.setSpacing(4);
\r
270 return generalPanel;
\r
273 private VerticalPanel createSharingPanel() {
\r
274 VerticalPanel permPanel = new VerticalPanel();
\r
276 permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);
\r
277 permPanel.add(permList);
\r
279 if (file.getInheritedPermissionsFrom() == null) {
\r
280 HorizontalPanel permButtons = new HorizontalPanel();
\r
281 Button add = new Button("Add Group", new ClickHandler() {
\r
283 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
284 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
\r
286 permList.updatePermissionTable();
\r
289 permButtons.add(add);
\r
290 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
292 final Button addUser = new Button("Add User", new ClickHandler() {
\r
294 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
295 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
\r
297 permList.updatePermissionTable();
\r
300 permButtons.add(addUser);
\r
301 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
303 permButtons.setSpacing(8);
\r
304 permButtons.addStyleName("pithos-TabPanelBottom");
\r
305 permPanel.add(permButtons);
\r
308 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
\r
309 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
310 "distribute this file and that it does not violate the Terms of Use.", true);
\r
311 readForAllNote.setVisible(false);
\r
312 readForAllNote.setStylePrimaryName("pithos-readForAllNote");
\r
314 readForAll = new CheckBox();
\r
315 readForAll.setValue(file.isPublished());
\r
316 readForAll.addClickHandler(new ClickHandler() {
\r
318 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
319 readForAllNote.setVisible(readForAll.getValue());
\r
323 // Only show the read for all permission if the user is the owner.
\r
324 if (file.getOwner().equals(app.getUsername())) {
\r
325 final HorizontalPanel permForAll = new HorizontalPanel();
\r
326 permForAll.add(new Label("Public"));
\r
327 permForAll.add(readForAll);
\r
328 permForAll.setSpacing(8);
\r
329 permForAll.addStyleName("pithos-TabPanelBottom");
\r
330 permForAll.add(readForAllNote);
\r
331 permPanel.add(permForAll);
\r
334 if (file.isPublished()) {
\r
335 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
336 pathPanel.setWidth("100%");
\r
337 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
338 pathPanel.add(new Label("Link"));
\r
339 pathPanel.setSpacing(8);
\r
340 pathPanel.addStyleName("pithos-TabPanelBottom");
\r
342 TextBox path = new TextBox();
\r
343 path.setWidth("100%");
\r
344 path.addClickHandler(new ClickHandler() {
\r
346 public void onClick(ClickEvent event) {
\r
347 Pithos.enableIESelection();
\r
348 ((TextBox) event.getSource()).selectAll();
\r
349 Pithos.preventIESelection();
\r
352 path.setText(Window.Location.getHost() + file.getPublicUri());
\r
353 path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
354 path.setWidth("100%");
\r
355 path.setReadOnly(true);
\r
356 pathPanel.add(path);
\r
357 permPanel.add(pathPanel);
\r
363 private VerticalPanel createVersionPanel(List<Version> versions) {
\r
364 VerticalPanel versionPanel = new VerticalPanel();
\r
365 VersionsList verList = new VersionsList(app, this, images, file, versions);
\r
366 versionPanel.add(verList);
\r
367 return versionPanel;
\r
371 * Accepts any change and updates the file
\r
375 protected void accept() {
\r
376 String newFilename = null;
\r
378 final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
\r
380 if (!name.getText().trim().equals(file.getName())) {
\r
381 newFilename = name.getText().trim();
\r
384 //only update the read for all perm if the user is the owner
\r
385 Boolean published = null;
\r
386 if (readForAll.getValue() != file.isPublished())
\r
387 if (file.getOwner().equals(app.getUsername()))
\r
388 published = readForAll.getValue();
\r
389 final Boolean finalPublished = published;
\r
391 String[] tagset = null;
\r
392 if (!tags.getText().equals(initialTagText))
\r
393 tagset = tags.getText().split(",");
\r
394 final String[] newTags = tagset;
\r
396 if (newFilename != null) {
\r
397 final String path = file.getParent().getUri() + "/" + newFilename;
\r
398 PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
\r
400 public void onSuccess(@SuppressWarnings("unused") Resource result) {
\r
401 updateMetaData(app.getApiPath(), app.getUsername(), path + "?update=", newTags, finalPublished, perms);
\r
405 public void onError(Throwable t) {
\r
407 app.displayError("System error modifying file:" + t.getMessage());
\r
410 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
411 updateFile.setHeader("X-Move-From", file.getUri());
\r
412 updateFile.setHeader("Content-Type", file.getContentType());
\r
413 Scheduler.get().scheduleDeferred(updateFile);
\r
416 updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished, perms);
\r
419 protected void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published, Map<String, Boolean[]> newPermissions) {
\r
420 if (newTags != null || published != null || newPermissions != null) {
\r
421 PostRequest updateFile = new PostRequest(api, owner, path) {
\r
423 public void onSuccess(@SuppressWarnings("unused") Resource result) {
\r
424 app.updateFolder(file.getParent(), true, null);
\r
428 public void onError(Throwable t) {
\r
430 app.displayError("System error modifying file:" + t.getMessage());
\r
433 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
434 if (newTags != null)
\r
435 for (String t : newTags)
\r
436 updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");
\r
437 if (published != null)
\r
438 updateFile.setHeader("X-Object-Public", published.toString());
\r
439 if (newPermissions != null) {
\r
440 String readPermHeader = "read=";
\r
441 String writePermHeader = "write=";
\r
442 for (String u : newPermissions.keySet()) {
\r
443 Boolean[] p = newPermissions.get(u);
\r
444 if (p[0] != null && p[0])
\r
445 readPermHeader += u + ",";
\r
446 if (p[1] != null && p[1])
\r
447 writePermHeader += u + ",";
\r
449 if (readPermHeader.endsWith("="))
\r
450 readPermHeader = "";
\r
451 else if (readPermHeader.endsWith(","))
\r
452 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
\r
453 if (writePermHeader.endsWith("="))
\r
454 writePermHeader = "";
\r
455 else if (writePermHeader.endsWith(","))
\r
456 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
\r
457 String permHeader = readPermHeader + ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? ";" : "") + writePermHeader;
\r
458 if (permHeader.length() == 0)
\r
460 updateFile.setHeader("X-Object-Sharing", permHeader);
\r
462 Scheduler.get().scheduleDeferred(updateFile);
\r
465 app.updateFolder(file.getParent(), true, null);
\r
468 private void removeAllOldVersions() {
\r