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
46 import java.util.HashMap;
\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.FlexTable;
\r
64 import com.google.gwt.user.client.ui.FocusPanel;
\r
65 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
66 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
67 import com.google.gwt.user.client.ui.Image;
\r
68 import com.google.gwt.user.client.ui.Label;
\r
69 import com.google.gwt.user.client.ui.TextBox;
\r
70 import com.google.gwt.user.client.ui.VerticalPanel;
\r
73 * The 'File properties' dialog box implementation.
\r
76 public class FilePropertiesDialog extends AbstractPropertiesDialog {
\r
78 protected PermissionsList permList;
\r
80 protected CheckBox readForAll;
\r
83 * An image bundle for this widgets images.
\r
85 public interface Images extends MessagePanel.Images {
\r
87 @Source("gr/grnet/pithos/resources/edit_user.png")
\r
88 ImageResource permUser();
\r
90 @Source("gr/grnet/pithos/resources/groups22.png")
\r
91 ImageResource permGroup();
\r
93 @Source("gr/grnet/pithos/resources/editdelete.png")
\r
94 ImageResource delete();
\r
96 @Source("gr/grnet/pithos/resources/db_update.png")
\r
97 ImageResource restore();
\r
99 @Source("gr/grnet/pithos/resources/folder_inbox.png")
\r
100 ImageResource download();
\r
104 * The widget that holds the name of the file.
\r
106 private TextBox name = new TextBox();
\r
110 Images images = GWT.create(Images.class);
\r
112 FlexTable metaTable;
\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(@SuppressWarnings("unused") 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(@SuppressWarnings("unused") 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
210 name.setWidth("100%");
\r
211 name.setText(file.getName());
\r
212 generalTable.setWidget(0, 1, name);
\r
213 if(file.getParent() != null)
\r
214 generalTable.setText(1, 1, file.getParent().getName());
\r
216 generalTable.setText(1, 1, "-");
\r
217 generalTable.setText(2, 1, file.getOwner());
\r
219 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
220 generalTable.setText(3, 1, file.getLastModified() != null ? formatter.format(file.getLastModified()) : "");
\r
222 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
223 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
224 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
225 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
226 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
\r
227 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
228 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
229 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
230 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
231 generalTable.setCellSpacing(4);
\r
233 generalPanel.add(generalTable);
\r
235 HorizontalPanel metaTitlePanel = new HorizontalPanel();
\r
236 metaTitlePanel.setSpacing(5);
\r
237 Label meta = new Label("Meta data");
\r
238 meta.addStyleName("pithos-metaTitle");
\r
239 metaTitlePanel.add(meta);
\r
241 Image plus = new Image("images/plus.png");
\r
242 plus.addStyleName("pithos-addMetaImg");
\r
243 metaTitlePanel.add(plus);
\r
245 generalPanel.add(metaTitlePanel);
\r
247 metaTable = new FlexTable();
\r
248 metaTable.setCellSpacing(0);
\r
249 metaTable.setHTML(0, 0, "Name");
\r
250 metaTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
251 metaTable.setText(0, 1, "Value");
\r
252 metaTable.getFlexCellFormatter().setStyleName(0, 1, "props-labels");
\r
254 for (String metaKey : file.getMeta().keySet()) {
\r
255 addFormLine(metaTable, rows++, metaKey, file.getMeta().get(metaKey));
\r
257 if (rows == 1) //If no meta add an empty line
\r
258 addFormLine(metaTable, rows++, "", "");
\r
260 plus.addClickHandler(new ClickHandler() {
\r
263 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
264 addFormLine(metaTable, metaTable.getRowCount(), "", "");
\r
268 generalPanel.add(metaTable);
\r
269 generalPanel.setSpacing(4);
\r
270 return generalPanel;
\r
273 void addFormLine(final FlexTable table, int row, String _name, String _value) {
\r
274 TextBox nameBox = new TextBox();
\r
275 nameBox.setText(_name);
\r
276 nameBox.addStyleName("pithos-metaName");
\r
277 table.setWidget(row, 0, nameBox);
\r
278 table.getFlexCellFormatter().setStyleName(1, 0, "props-values");
\r
280 TextBox valueBox = new TextBox();
\r
281 valueBox.setText(_value);
\r
282 valueBox.addStyleName("pithos-metaValue");
\r
283 table.setWidget(row, 1, valueBox);
\r
284 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
286 Image delete = new Image("images/delete.png");
\r
287 delete.addStyleName("pithos-metaDeleteImg");
\r
288 delete.addClickHandler(new ClickHandler() {
\r
291 public void onClick(ClickEvent event) {
\r
292 int rowIndex = table.getCellForEvent(event).getRowIndex();
\r
293 table.removeRow(rowIndex);
\r
296 table.setWidget(row, 2, delete);
\r
299 private VerticalPanel createSharingPanel() {
\r
300 VerticalPanel permPanel = new VerticalPanel();
\r
302 permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);
\r
303 permPanel.add(permList);
\r
305 if (file.getInheritedPermissionsFrom() == null) {
\r
306 HorizontalPanel permButtons = new HorizontalPanel();
\r
307 Button add = new Button("Add Group", new ClickHandler() {
\r
309 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
310 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
\r
312 permList.updatePermissionTable();
\r
315 add.addStyleName("button");
\r
316 permButtons.add(add);
\r
317 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
319 final Button addUser = new Button("Add User", new ClickHandler() {
\r
321 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
322 PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
\r
324 permList.updatePermissionTable();
\r
327 addUser.addStyleName("button");
\r
328 permButtons.add(addUser);
\r
329 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
331 permButtons.setSpacing(8);
\r
332 permButtons.addStyleName("pithos-TabPanelBottom");
\r
333 permPanel.add(permButtons);
\r
336 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
\r
337 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
338 "distribute this file and that it does not violate the Terms of Use.", true);
\r
339 readForAllNote.setVisible(false);
\r
340 readForAllNote.setStylePrimaryName("pithos-readForAllNote");
\r
342 readForAll = new CheckBox();
\r
343 readForAll.setValue(file.isPublished());
\r
344 readForAll.addClickHandler(new ClickHandler() {
\r
346 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
\r
347 readForAllNote.setVisible(readForAll.getValue());
\r
351 // Only show the read for all permission if the user is the owner.
\r
352 if (file.getOwner().equals(app.getUsername())) {
\r
353 final HorizontalPanel permForAll = new HorizontalPanel();
\r
354 permForAll.add(new Label("Public"));
\r
355 permForAll.add(readForAll);
\r
356 permForAll.setSpacing(8);
\r
357 permForAll.addStyleName("pithos-TabPanelBottom");
\r
358 permForAll.add(readForAllNote);
\r
359 permPanel.add(permForAll);
\r
362 if (file.isPublished()) {
\r
363 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
364 pathPanel.setWidth("100%");
\r
365 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
366 pathPanel.add(new Label("Link"));
\r
367 pathPanel.setSpacing(8);
\r
368 pathPanel.addStyleName("pithos-TabPanelBottom");
\r
370 TextBox path = new TextBox();
\r
371 path.setWidth("100%");
\r
372 path.addClickHandler(new ClickHandler() {
\r
374 public void onClick(ClickEvent event) {
\r
375 Pithos.enableIESelection();
\r
376 ((TextBox) event.getSource()).selectAll();
\r
377 Pithos.preventIESelection();
\r
380 path.setText(Window.Location.getHost() + file.getPublicUri());
\r
381 path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
382 path.setWidth("100%");
\r
383 path.setReadOnly(true);
\r
384 pathPanel.add(path);
\r
385 permPanel.add(pathPanel);
\r
391 VerticalPanel createVersionPanel(List<Version> versions) {
\r
392 VerticalPanel versionPanel = new VerticalPanel();
\r
393 VersionsList verList = new VersionsList(app, this, images, file, versions);
\r
394 versionPanel.add(verList);
\r
395 return versionPanel;
\r
399 * Accepts any change and updates the file
\r
403 protected void accept() {
\r
404 String newFilename = null;
\r
406 final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
\r
408 if (!name.getText().trim().equals(file.getName())) {
\r
409 newFilename = name.getText().trim();
\r
412 //only update the read for all perm if the user is the owner
\r
413 Boolean published = null;
\r
414 if (readForAll.getValue() != file.isPublished())
\r
415 if (file.getOwner().equals(app.getUsername()))
\r
416 published = readForAll.getValue();
\r
417 final Boolean finalPublished = published;
\r
419 final Map<String, String> newMeta = new HashMap<String, String>();
\r
420 for (int row = 1; row < metaTable.getRowCount(); row++) {
\r
421 String key = ((TextBox) metaTable.getWidget(row, 0)).getText().trim();
\r
422 String value = ((TextBox) metaTable.getWidget(row, 1)).getText().trim();
\r
423 if (key.length() > 0 && value.length() > 0)
\r
424 newMeta.put(key, value);
\r
427 if (newFilename != null) {
\r
428 final String path = file.getParent().getUri() + "/" + newFilename;
\r
429 PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
\r
431 public void onSuccess(@SuppressWarnings("unused") Resource result) {
\r
432 updateMetaData(app.getApiPath(), file.getOwner(), path + "?update=", newMeta, finalPublished, perms);
\r
436 public void onError(Throwable t) {
\r
438 app.displayError("System error modifying file:" + t.getMessage());
\r
442 protected void onUnauthorized(@SuppressWarnings("unused") Response response) {
\r
443 app.sessionExpired();
\r
446 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
447 updateFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
\r
448 updateFile.setHeader("Content-Type", file.getContentType());
\r
449 Scheduler.get().scheduleDeferred(updateFile);
\r
452 updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newMeta, finalPublished, perms);
\r
455 protected void updateMetaData(String api, String owner, String path, Map<String, String> newMeta, Boolean published, Map<String, Boolean[]> newPermissions) {
\r
456 if (newMeta != null || published != null || newPermissions != null) {
\r
457 PostRequest updateFile = new PostRequest(api, owner, path) {
\r
459 public void onSuccess(@SuppressWarnings("unused") Resource result) {
\r
460 app.updateFolder(file.getParent(), true, null);
\r
464 public void onError(Throwable t) {
\r
466 app.displayError("System error modifying file:" + t.getMessage());
\r
470 protected void onUnauthorized(@SuppressWarnings("unused") Response response) {
\r
471 app.sessionExpired();
\r
474 updateFile.setHeader("X-Auth-Token", app.getToken());
\r
476 if (newMeta != null) {
\r
477 for (String t : file.getMeta().keySet()) {
\r
478 updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(t.trim()), "~");
\r
481 for (String key : newMeta.keySet())
\r
482 updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(key.trim()), URL.encodePathSegment(newMeta.get(key)));
\r
485 if (published != null)
\r
486 updateFile.setHeader("X-Object-Public", published.toString());
\r
487 if (newPermissions != null) {
\r
488 String readPermHeader = "read=";
\r
489 String writePermHeader = "write=";
\r
490 for (String u : newPermissions.keySet()) {
\r
491 Boolean[] p = newPermissions.get(u);
\r
492 if (p[0] != null && p[0])
\r
493 readPermHeader += u + ",";
\r
494 if (p[1] != null && p[1])
\r
495 writePermHeader += u + ",";
\r
497 if (readPermHeader.endsWith("="))
\r
498 readPermHeader = "";
\r
499 else if (readPermHeader.endsWith(","))
\r
500 readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
\r
501 if (writePermHeader.endsWith("="))
\r
502 writePermHeader = "";
\r
503 else if (writePermHeader.endsWith(","))
\r
504 writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
\r
505 String permHeader = readPermHeader + ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? ";" : "") + writePermHeader;
\r
506 if (permHeader.length() == 0)
\r
509 permHeader = URL.encodePathSegment(permHeader);
\r
510 updateFile.setHeader("X-Object-Sharing", permHeader);
\r
512 Scheduler.get().scheduleDeferred(updateFile);
\r
515 app.updateFolder(file.getParent(), true, null);
\r