2 * Copyright 2011-2012 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 gr.grnet.pithos.web.client.PithosDisclosurePanel.Style;
38 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
39 import gr.grnet.pithos.web.client.foldertree.AccountResource;
40 import gr.grnet.pithos.web.client.foldertree.File;
41 import gr.grnet.pithos.web.client.foldertree.Folder;
42 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
43 import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
44 import gr.grnet.pithos.web.client.foldertree.Resource;
45 import gr.grnet.pithos.web.client.grouptree.Group;
46 import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
47 import gr.grnet.pithos.web.client.grouptree.GroupTreeViewModel;
48 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView;
49 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
50 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
51 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
52 import gr.grnet.pithos.web.client.rest.DeleteRequest;
53 import gr.grnet.pithos.web.client.rest.GetRequest;
54 import gr.grnet.pithos.web.client.rest.HeadRequest;
55 import gr.grnet.pithos.web.client.rest.PutRequest;
56 import gr.grnet.pithos.web.client.rest.RestException;
58 import java.util.ArrayList;
59 import java.util.HashMap;
60 import java.util.Iterator;
61 import java.util.List;
64 import com.google.gwt.core.client.EntryPoint;
65 import com.google.gwt.core.client.GWT;
66 import com.google.gwt.core.client.Scheduler;
67 import com.google.gwt.core.client.Scheduler.RepeatingCommand;
68 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
69 import com.google.gwt.event.dom.client.ClickEvent;
70 import com.google.gwt.event.dom.client.ClickHandler;
71 import com.google.gwt.event.logical.shared.ResizeEvent;
72 import com.google.gwt.event.logical.shared.ResizeHandler;
73 import com.google.gwt.http.client.Request;
74 import com.google.gwt.http.client.RequestBuilder;
75 import com.google.gwt.http.client.RequestCallback;
76 import com.google.gwt.http.client.RequestException;
77 import com.google.gwt.http.client.Response;
78 import com.google.gwt.http.client.URL;
79 import com.google.gwt.i18n.client.Dictionary;
80 import com.google.gwt.i18n.client.NumberFormat;
81 import com.google.gwt.json.client.JSONArray;
82 import com.google.gwt.json.client.JSONObject;
83 import com.google.gwt.json.client.JSONParser;
84 import com.google.gwt.json.client.JSONString;
85 import com.google.gwt.json.client.JSONValue;
86 import com.google.gwt.resources.client.ClientBundle;
87 import com.google.gwt.resources.client.CssResource;
88 import com.google.gwt.resources.client.ImageResource;
89 import com.google.gwt.resources.client.ClientBundle.Source;
90 import com.google.gwt.resources.client.ImageResource.ImageOptions;
91 import com.google.gwt.user.client.Command;
92 import com.google.gwt.user.client.Cookies;
93 import com.google.gwt.user.client.Event;
94 import com.google.gwt.user.client.History;
95 import com.google.gwt.user.client.Window;
96 import com.google.gwt.user.client.ui.AbstractImagePrototype;
97 import com.google.gwt.user.client.ui.Button;
98 import com.google.gwt.user.client.ui.Composite;
99 import com.google.gwt.user.client.ui.FlowPanel;
100 import com.google.gwt.user.client.ui.HTML;
101 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
102 import com.google.gwt.user.client.ui.HasVerticalAlignment;
103 import com.google.gwt.user.client.ui.HorizontalPanel;
104 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
105 import com.google.gwt.user.client.ui.RootPanel;
106 import com.google.gwt.user.client.ui.VerticalPanel;
107 import com.google.gwt.view.client.SelectionChangeEvent;
108 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
109 import com.google.gwt.view.client.SingleSelectionModel;
112 * Entry point classes define <code>onModuleLoad()</code>.
114 public class Pithos implements EntryPoint, ResizeHandler {
116 public static final String HOME_CONTAINER = "pithos";
118 public static final String TRASH_CONTAINER = "trash";
120 public static final Configuration config = GWT.create(Configuration.class);
122 public interface Style extends CssResource {
123 String commandAnchor();
127 @ClassName("gwt-HTML")
131 public interface Resources extends ClientBundle {
132 @Source("Pithos.css")
136 public static Resources resources = GWT.create(Resources.class);
139 * Instantiate an application-level image bundle. This object will provide
140 * programmatic access to all the images needed by widgets.
142 static Images images = (Images) GWT.create(Images.class);
144 public String getUsername() {
148 public void setAccount(AccountResource acct) {
152 public AccountResource getAccount() {
156 public void updateFolder(Folder f, boolean showfiles, Command callback, final boolean openParent) {
157 folderTreeView.updateFolder(f, showfiles, callback, openParent);
160 public void updateGroupNode(Group group) {
161 groupTreeView.updateGroupNode(group);
164 public void updateMySharedRoot() {
165 mysharedTreeView.updateRoot();
168 public void updateSharedFolder(Folder f, boolean showfiles, Command callback) {
169 mysharedTreeView.updateFolder(f, showfiles, callback);
172 public void updateSharedFolder(Folder f, boolean showfiles) {
173 updateSharedFolder(f, showfiles, null);
176 public void updateOtherSharedFolder(Folder f, boolean showfiles) {
177 otherSharedTreeView.updateFolder(f, showfiles);
180 public MysharedTreeView getMySharedTreeView() {
181 return mysharedTreeView;
185 * An aggregate image bundle that pulls together all the images for this
186 * application into a single bundle.
188 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
190 @Source("gr/grnet/pithos/resources/document.png")
191 ImageResource folders();
193 @Source("gr/grnet/pithos/resources/advancedsettings.png")
194 @ImageOptions(width=32, height=32)
195 ImageResource tools();
198 private Throwable error;
201 * The Application Clipboard implementation;
203 private Clipboard clipboard = new Clipboard();
206 * The top panel that contains the menu bar.
208 private TopPanel topPanel;
211 * The panel that contains the various system messages.
213 private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
216 * The bottom panel that contains the status bar.
218 private StatusPanel statusPanel = null;
221 * The file list widget.
223 private FileList fileList;
226 * The tab panel that occupies the right side of the screen.
228 private VerticalPanel inner = new VerticalPanel();
232 * The split panel that will contain the left and right panels.
234 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
237 * The currently selected item in the application, for use by the Edit menu
238 * commands. Potential types are Folder, File, User and Group.
240 private Object currentSelection;
242 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
244 private String username = null;
247 * The authentication token of the current user.
249 private String token;
253 SingleSelectionModel<Folder> folderTreeSelectionModel;
254 FolderTreeViewModel folderTreeViewModel;
255 FolderTreeView folderTreeView;
257 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
258 MysharedTreeViewModel mysharedTreeViewModel;
259 MysharedTreeView mysharedTreeView = null;;
261 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
262 OtherSharedTreeViewModel otherSharedTreeViewModel;
263 OtherSharedTreeView otherSharedTreeView = null;
265 GroupTreeViewModel groupTreeViewModel;
266 GroupTreeView groupTreeView;
268 TreeView selectedTree;
269 protected AccountResource account;
273 List<Composite> treeViews = new ArrayList<Composite>();
275 @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
277 public Button upload;
279 private HTML numOfFiles;
281 private Toolbar toolbar;
283 private FileUploadDialog fileUploadDialog = new FileUploadDialog(this);
286 public void onModuleLoad() {
287 if (parseUserCredentials())
291 private void initialize() {
292 resources.pithosCss().ensureInjected();
293 boolean bareContent = Window.Location.getParameter("noframe") != null;
294 String contentWidth = bareContent ? "100%" : "75%";
296 VerticalPanel outer = new VerticalPanel();
297 outer.setWidth("100%");
299 outer.addStyleName("pithos-outer");
303 topPanel = new TopPanel(this, Pithos.images);
304 topPanel.setWidth("100%");
306 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
309 messagePanel.setVisible(false);
310 outer.add(messagePanel);
311 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
312 outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
314 HorizontalPanel header = new HorizontalPanel();
315 header.addStyleName("pithos-header");
316 header.setWidth(contentWidth);
318 header.addStyleName("pithos-header-noframe");
319 upload = new Button("Upload", new ClickHandler() {
321 public void onClick(ClickEvent event) {
322 if (getSelection() != null)
323 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
326 upload.addStyleName("pithos-uploadButton");
328 header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
329 header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
331 toolbar = new Toolbar(this);
333 header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
334 header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
336 HorizontalPanel folderStatistics = new HorizontalPanel();
337 folderStatistics.addStyleName("pithos-folderStatistics");
338 numOfFiles = new HTML();
339 folderStatistics.add(numOfFiles);
340 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
341 HTML numOfFilesLabel = new HTML(" Files");
342 folderStatistics.add(numOfFilesLabel);
343 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
344 header.add(folderStatistics);
345 header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
346 header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
347 header.setCellWidth(folderStatistics, "40px");
349 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
350 // Inner contains the various lists
351 inner.sinkEvents(Event.ONCONTEXTMENU);
352 inner.setWidth("100%");
354 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
355 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
357 public void onSelectionChange(SelectionChangeEvent event) {
358 if (folderTreeSelectionModel.getSelectedObject() != null) {
359 deselectOthers(folderTreeView, folderTreeSelectionModel);
360 applyPermissions(folderTreeSelectionModel.getSelectedObject());
361 Folder f = folderTreeSelectionModel.getSelectedObject();
362 updateFolder(f, true, new Command() {
365 public void execute() {
369 showRelevantToolbarButtons();
372 if (getSelectedTree().equals(folderTreeView))
373 setSelectedTree(null);
374 if (getSelectedTree() == null)
375 showRelevantToolbarButtons();
379 selectionModels.add(folderTreeSelectionModel);
381 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
382 folderTreeView = new FolderTreeView(folderTreeViewModel);
383 treeViews.add(folderTreeView);
385 fileList = new FileList(this, images);
388 inner.add(createUploadArea());
390 trees = new VerticalPanel();
391 trees.setWidth("100%");
393 // Add the left and right panels to the split panel.
394 splitPanel.setLeftWidget(trees);
395 FlowPanel right = new FlowPanel();
396 right.getElement().setId("rightPanel");
398 splitPanel.setRightWidget(right);
399 splitPanel.setSplitPosition("219px");
400 splitPanel.setSize("100%", "100%");
401 splitPanel.addStyleName("pithos-splitPanel");
402 splitPanel.setWidth(contentWidth);
403 outer.add(splitPanel);
404 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
407 statusPanel = new StatusPanel();
408 statusPanel.setWidth("100%");
409 outer.add(statusPanel);
410 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
413 splitPanel.addStyleName("pithos-splitPanel-noframe");
415 // Hook the window resize event, so that we can adjust the UI.
416 Window.addResizeHandler(this);
417 // Clear out the window's built-in margin, because we want to take
418 // advantage of the entire client area.
419 Window.setMargin("0px");
420 // Finally, add the outer panel to the RootPanel, so that it will be
422 RootPanel.get().add(outer);
423 // Call the window resized handler to get the initial sizes setup. Doing
424 // this in a deferred command causes it to occur after all widgets'
425 // sizes have been computed by the browser.
426 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
429 public void execute() {
430 onWindowResized(Window.getClientHeight());
434 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
436 public void execute() {
437 fetchAccount(new Command() {
440 public void execute() {
441 if (!account.hasHomeContainer())
442 createHomeContainer(account, this);
443 else if (!account.hasTrashContainer())
444 createTrashContainer(this);
446 for (Folder f : account.getContainers())
447 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
451 trees.add(folderTreeView);
452 folderTreeViewModel.initialize(account, new Command() {
455 public void execute() {
456 createMySharedTree();
460 HorizontalPanel separator = new HorizontalPanel();
461 separator.addStyleName("pithos-statisticsSeparator");
462 separator.add(new HTML(""));
463 trees.add(separator);
465 groupTreeViewModel = new GroupTreeViewModel(Pithos.this);
466 groupTreeView = new GroupTreeView(groupTreeViewModel);
467 treeViews.add(groupTreeView);
468 trees.add(groupTreeView);
469 folderTreeView.showStatistics(account);
476 Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
479 public boolean execute() {
480 Folder f = getSelection();
482 if (getSelectedTree().equals(folderTreeView))
483 updateFolder(f, true, null, false);
484 else if (getSelectedTree().equals(mysharedTreeView))
485 updateSharedFolder(f, true);
492 public void applyPermissions(Folder f) {
495 upload.setEnabled(false);
499 Boolean[] perms = f.getPermissions().get(username);
500 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
501 upload.setEnabled(true);
505 upload.setEnabled(false);
511 upload.setEnabled(false);
516 @SuppressWarnings({ "rawtypes", "unchecked" })
517 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
518 selectedTree = _selectedTree;
520 for (SingleSelectionModel s : selectionModels)
521 if (!s.equals(model) && s.getSelectedObject() != null)
522 s.setSelected(s.getSelectedObject(), false);
525 public void showFiles(final Folder f) {
526 Set<File> files = f.getFiles();
530 public void showFiles(Set<File> files) {
531 fileList.setFiles(new ArrayList<File>(files));
535 * Parse and store the user credentials to the appropriate fields.
537 private boolean parseUserCredentials() {
538 Configuration conf = (Configuration) GWT.create(Configuration.class);
539 Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
540 String cookie = otherProperties.get("authCookie");
541 String auth = Cookies.getCookie(cookie);
546 if (auth.startsWith("\""))
547 auth = auth.substring(1);
548 if (auth.endsWith("\""))
549 auth = auth.substring(0, auth.length() - 1);
550 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
551 if (authSplit.length != 2) {
555 username = authSplit[0];
556 token = authSplit[1];
558 String gotoUrl = Window.Location.getParameter("goto");
559 if (gotoUrl != null && gotoUrl.length() > 0) {
560 Window.Location.assign(gotoUrl);
567 * Redirect the user to the login page for authentication.
569 protected void authenticateUser() {
570 Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
571 Window.Location.assign(otherProperties.get("loginUrl") + Window.Location.getHref());
574 protected void fetchAccount(final Command callback) {
575 String path = "?format=json";
577 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
579 public void onSuccess(AccountResource _result) {
581 if (callback != null)
586 public void onError(Throwable t) {
587 GWT.log("Error getting account", t);
589 if (t instanceof RestException)
590 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
592 displayError("System error fetching user data: " + t.getMessage());
596 protected void onUnauthorized(Response response) {
600 getAccount.setHeader("X-Auth-Token", token);
601 Scheduler.get().scheduleDeferred(getAccount);
604 public void updateStatistics() {
605 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
608 public void onSuccess(AccountResource _result) {
609 folderTreeView.showStatistics(account);
613 public void onError(Throwable t) {
614 GWT.log("Error getting account", t);
616 if (t instanceof RestException)
617 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
619 displayError("System error fetching user data: " + t.getMessage());
623 protected void onUnauthorized(Response response) {
627 headAccount.setHeader("X-Auth-Token", token);
628 Scheduler.get().scheduleDeferred(headAccount);
631 protected void createHomeContainer(final AccountResource _account, final Command callback) {
632 String path = "/" + Pithos.HOME_CONTAINER;
633 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
635 public void onSuccess(Resource result) {
636 if (!_account.hasTrashContainer())
637 createTrashContainer(callback);
639 fetchAccount(callback);
643 public void onError(Throwable t) {
644 GWT.log("Error creating pithos", t);
646 if (t instanceof RestException)
647 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
649 displayError("System error Error creating pithos: " + t.getMessage());
653 protected void onUnauthorized(Response response) {
657 createPithos.setHeader("X-Auth-Token", getToken());
658 Scheduler.get().scheduleDeferred(createPithos);
661 protected void createTrashContainer(final Command callback) {
662 String path = "/" + Pithos.TRASH_CONTAINER;
663 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
665 public void onSuccess(Resource result) {
666 fetchAccount(callback);
670 public void onError(Throwable t) {
671 GWT.log("Error creating pithos", t);
673 if (t instanceof RestException)
674 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
676 displayError("System error Error creating pithos: " + t.getMessage());
680 protected void onUnauthorized(Response response) {
684 createPithos.setHeader("X-Auth-Token", getToken());
685 Scheduler.get().scheduleDeferred(createPithos);
689 * Creates an HTML fragment that places an image & caption together, for use
692 * @param imageProto an image prototype for an image
693 * @param caption the group caption
694 * @return the header HTML fragment
696 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
697 String captionHTML = "<table class='caption' cellpadding='0' "
698 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
699 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
700 + caption + "</b></td></tr></table>";
704 protected void onWindowResized(int height) {
705 // Adjust the split panel to take up the available room in the window.
706 int newHeight = height - splitPanel.getAbsoluteTop();
709 splitPanel.setHeight("" + newHeight);
710 inner.setHeight("" + newHeight);
714 public void onResize(ResizeEvent event) {
715 int height = event.getHeight();
716 onWindowResized(height);
720 * Display an error message.
722 * @param msg the message to display
724 public void displayError(String msg) {
725 messagePanel.displayError(msg);
729 * Display a warning message.
731 * @param msg the message to display
733 public void displayWarning(String msg) {
734 messagePanel.displayWarning(msg);
738 * Display an informational message.
740 * @param msg the message to display
742 public void displayInformation(String msg) {
743 messagePanel.displayInformation(msg);
747 * Retrieve the fileList.
749 * @return the fileList
751 public FileList getFileList() {
756 * Retrieve the topPanel.
758 * @return the topPanel
760 TopPanel getTopPanel() {
765 * Retrieve the clipboard.
767 * @return the clipboard
769 public Clipboard getClipboard() {
773 public StatusPanel getStatusPanel() {
777 public String getToken() {
781 public static native void preventIESelection() /*-{
782 $doc.body.onselectstart = function () { return false; };
785 public static native void enableIESelection() /*-{
786 if ($doc.body.onselectstart != null)
787 $doc.body.onselectstart = null;
791 * @return the absolute path of the API root URL
793 public String getApiPath() {
794 Configuration conf = (Configuration) GWT.create(Configuration.class);
795 return conf.apiPath();
799 * History support for folder navigation
800 * adds a new browser history entry
804 public void updateHistory(String key){
805 // Replace any whitespace of the initial string to "+"
806 // String result = key.replaceAll("\\s","+");
807 // Add a new browser history entry.
808 // History.newItem(result);
809 History.newItem(key);
812 public void deleteFolder(final Folder folder, final Command callback) {
813 String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
814 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
815 builder.setHeader("X-Auth-Token", getToken());
817 builder.sendRequest("", new RequestCallback() {
819 public void onResponseReceived(Request request, Response response) {
820 if (response.getStatusCode() == Response.SC_OK) {
821 JSONValue json = JSONParser.parseStrict(response.getText());
822 JSONArray array = json.isArray();
825 deleteObject(folder, i, array, callback);
831 public void onError(Request request, Throwable exception) {
833 displayError("System error unable to delete folder: " + exception.getMessage());
837 catch (RequestException e) {
841 void deleteObject(final Folder folder, final int i, final JSONArray array, final Command callback) {
842 if (i < array.size()) {
843 JSONObject o = array.get(i).isObject();
844 if (o != null && !o.containsKey("subdir")) {
845 JSONString name = o.get("name").isString();
846 String path = "/" + folder.getContainer() + "/" + name.stringValue();
847 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), URL.encode(path)) {
849 public void onSuccess(Resource result) {
850 deleteObject(folder, i + 1, array, callback);
854 public void onError(Throwable t) {
857 displayError("System error unable to delete folder: " + t.getMessage());
861 protected void onUnauthorized(Response response) {
865 delete.setHeader("X-Auth-Token", getToken());
866 Scheduler.get().scheduleDeferred(delete);
868 else if (o != null) {
869 String subdir = o.get("subdir").isString().stringValue();
870 subdir = subdir.substring(0, subdir.length() - 1);
871 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
872 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
873 builder.setHeader("X-Auth-Token", getToken());
875 builder.sendRequest("", new RequestCallback() {
877 public void onResponseReceived(Request request, Response response) {
878 if (response.getStatusCode() == Response.SC_OK) {
879 JSONValue json = JSONParser.parseStrict(response.getText());
880 JSONArray array2 = json.isArray();
881 if (array2 != null) {
882 int l = array.size();
883 for (int j=0; j<array2.size(); j++) {
884 array.set(l++, array2.get(j));
887 deleteObject(folder, i + 1, array, callback);
892 public void onError(Request request, Throwable exception) {
894 displayError("System error unable to delete folder: " + exception.getMessage());
898 catch (RequestException e) {
903 String path = folder.getUri();
904 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), URL.encode(path)) {
906 public void onSuccess(Resource result) {
907 updateFolder(folder.getParent(), true, new Command() {
910 public void execute() {
911 folderTreeSelectionModel.setSelected(folder.getParent(), true);
913 if (callback != null)
920 public void onError(Throwable t) {
923 if (t instanceof RestException) {
924 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
925 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
930 displayError("System error unable to delete folder: " + t.getMessage());
934 protected void onUnauthorized(Response response) {
938 deleteFolder.setHeader("X-Auth-Token", getToken());
939 Scheduler.get().scheduleDeferred(deleteFolder);
943 public FolderTreeView getFolderTreeView() {
944 return folderTreeView;
947 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
948 if (iter.hasNext()) {
949 File file = iter.next();
950 String path = targetUri + "/" + file.getName();
951 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
953 public void onSuccess(Resource result) {
954 copyFiles(iter, targetUsername, targetUri, callback);
958 public void onError(Throwable t) {
961 if (t instanceof RestException) {
962 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
965 displayError("System error unable to copy file: "+t.getMessage());
969 protected void onUnauthorized(Response response) {
973 copyFile.setHeader("X-Auth-Token", getToken());
974 copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
975 if (!file.getOwner().equals(targetUsername))
976 copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwner()));
977 copyFile.setHeader("Content-Type", file.getContentType());
978 Scheduler.get().scheduleDeferred(copyFile);
980 else if (callback != null) {
985 public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
986 if (iter.hasNext()) {
987 final Folder f = iter.next();
988 copyFolder(f, targetUsername, targetUri, new Command() {
991 public void execute() {
992 copySubfolders(iter, targetUsername, targetUri, callback);
996 else if (callback != null) {
1001 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
1002 String path = targetUri + "/" + f.getName();
1003 PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
1005 public void onSuccess(Resource result) {
1006 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
1009 public void onSuccess(final Folder _f) {
1010 Iterator<File> iter = _f.getFiles().iterator();
1011 copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
1013 public void execute() {
1014 Iterator<Folder> iterf = _f.getSubfolders().iterator();
1015 copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1021 public void onError(Throwable t) {
1024 if (t instanceof RestException) {
1025 displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1028 displayError("System error getting folder: " + t.getMessage());
1032 protected void onUnauthorized(Response response) {
1036 getFolder.setHeader("X-Auth-Token", getToken());
1037 Scheduler.get().scheduleDeferred(getFolder);
1041 public void onError(Throwable t) {
1044 if (t instanceof RestException) {
1045 displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1048 displayError("System error creating folder: " + t.getMessage());
1052 protected void onUnauthorized(Response response) {
1056 createFolder.setHeader("X-Auth-Token", getToken());
1057 createFolder.setHeader("Accept", "*/*");
1058 createFolder.setHeader("Content-Length", "0");
1059 createFolder.setHeader("Content-Type", "application/folder");
1060 Scheduler.get().scheduleDeferred(createFolder);
1063 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1064 selectionModels.add(model);
1067 public OtherSharedTreeView getOtherSharedTreeView() {
1068 return otherSharedTreeView;
1071 public void updateTrash(boolean showFiles, Command callback) {
1072 updateFolder(trash, showFiles, callback, true);
1075 public void updateGroupsNode() {
1076 groupTreeView.updateGroupNode(null);
1079 public Group addGroup(String groupname) {
1080 Group newGroup = new Group(groupname);
1081 account.addGroup(newGroup);
1082 groupTreeView.updateGroupNode(null);
1086 public void removeGroup(Group group) {
1087 account.removeGroup(group);
1091 public TreeView getSelectedTree() {
1092 return selectedTree;
1095 public void setSelectedTree(TreeView selected) {
1096 selectedTree = selected;
1099 public Folder getSelection() {
1100 if (selectedTree != null)
1101 return selectedTree.getSelection();
1105 public void showFolderStatistics(int folderFileCount) {
1106 numOfFiles.setHTML(String.valueOf(folderFileCount));
1109 public GroupTreeView getGroupTreeView() {
1110 return groupTreeView;
1113 public void sessionExpired() {
1114 new SessionExpiredDialog(this).center();
1117 public void updateRootFolder(Command callback) {
1118 updateFolder(account.getPithos(), false, callback, true);
1121 void createMySharedTree() {
1122 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1123 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1125 public void onSelectionChange(SelectionChangeEvent event) {
1126 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1127 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1128 upload.setEnabled(false);
1129 disableUploadArea();
1130 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1131 showRelevantToolbarButtons();
1134 if (getSelectedTree().equals(mysharedTreeView))
1135 setSelectedTree(null);
1136 if (getSelectedTree() == null)
1137 showRelevantToolbarButtons();
1141 selectionModels.add(mysharedTreeSelectionModel);
1142 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1143 mysharedTreeViewModel.initialize(new Command() {
1146 public void execute() {
1147 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1148 trees.insert(mysharedTreeView, 2);
1149 treeViews.add(mysharedTreeView);
1150 createOtherSharedTree();
1155 void createOtherSharedTree() {
1156 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1157 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1159 public void onSelectionChange(SelectionChangeEvent event) {
1160 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1161 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1162 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1163 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1164 showRelevantToolbarButtons();
1167 if (getSelectedTree().equals(otherSharedTreeView))
1168 setSelectedTree(null);
1169 if (getSelectedTree() == null)
1170 showRelevantToolbarButtons();
1174 selectionModels.add(otherSharedTreeSelectionModel);
1175 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1176 otherSharedTreeViewModel.initialize(new Command() {
1179 public void execute() {
1180 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1181 trees.insert(otherSharedTreeView, 1);
1182 treeViews.add(otherSharedTreeView);
1187 public native void log1(String message)/*-{
1188 $wnd.console.log(message);
1191 public String getErrorData() {
1193 return error.toString();
1197 public void setError(Throwable t) {
1201 public void showRelevantToolbarButtons() {
1202 toolbar.showRelevantButtons();
1205 public FileUploadDialog getFileUploadDialog() {
1206 if (fileUploadDialog == null)
1207 fileUploadDialog = new FileUploadDialog(this);
1208 return fileUploadDialog;
1211 public void hideUploadIndicator() {
1212 upload.removeStyleName("pithos-uploadButton-loading");
1213 upload.setTitle("");
1216 public void showUploadIndicator() {
1217 upload.addStyleName("pithos-uploadButton-loading");
1218 upload.setTitle("Upload in progress. Click for details.");
1221 public void scheduleFolderHeadCommand(final Folder folder, final Command callback) {
1222 if (folder == null) {
1223 if (callback != null)
1227 HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, getApiPath(), folder.getOwner(), folder.getUri(), folder) {
1230 public void onSuccess(Folder _result) {
1231 if (callback != null)
1236 public void onError(Throwable t) {
1237 if (t instanceof RestException) {
1238 if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
1239 final String path = folder.getUri();
1240 PutRequest newFolder = new PutRequest(getApiPath(), folder.getOwner(), path) {
1242 public void onSuccess(Resource _result) {
1243 scheduleFolderHeadCommand(folder, callback);
1247 public void onError(Throwable _t) {
1250 if(_t instanceof RestException){
1251 displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
1254 displayError("System error creating folder: " + _t.getMessage());
1258 protected void onUnauthorized(Response response) {
1262 newFolder.setHeader("X-Auth-Token", getToken());
1263 newFolder.setHeader("Content-Type", "application/folder");
1264 newFolder.setHeader("Accept", "*/*");
1265 newFolder.setHeader("Content-Length", "0");
1266 Scheduler.get().scheduleDeferred(newFolder);
1269 displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
1272 displayError("System error heading folder: " + t.getMessage());
1274 GWT.log("Error heading folder", t);
1279 protected void onUnauthorized(Response response) {
1283 headFolder.setHeader("X-Auth-Token", getToken());
1284 Scheduler.get().scheduleDeferred(headFolder);
1288 public void scheduleFileHeadCommand(File f, final Command callback) {
1289 HeadRequest<File> headFile = new HeadRequest<File>(File.class, getApiPath(), f.getOwner(), f.getUri(), f) {
1292 public void onSuccess(File _result) {
1293 if (callback != null)
1298 public void onError(Throwable t) {
1299 GWT.log("Error heading file", t);
1301 if (t instanceof RestException)
1302 displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
1304 displayError("System error heading file: " + t.getMessage());
1308 protected void onUnauthorized(Response response) {
1312 headFile.setHeader("X-Auth-Token", getToken());
1313 Scheduler.get().scheduleDeferred(headFile);
1316 public boolean isMySharedSelected() {
1317 return getSelectedTree().equals(getMySharedTreeView());
1320 private FlowPanel createUploadArea() {
1321 FlowPanel area = new FlowPanel();
1322 area.getElement().setId("container");
1323 HTML list = new HTML();
1324 list.getElement().setId("filelist");
1326 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
1329 public void execute() {
1330 // setupUploadArea(Pithos.this, getToken());
1337 private Folder getUploadFolder() {
1338 if (folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1339 return getSelection();
1344 private void updateUploadFolder() {
1345 if (folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1346 Folder f = getSelection();
1347 if (getSelectedTree().equals(getFolderTreeView()))
1348 updateFolder(f, true, new Command() {
1351 public void execute() {
1356 updateOtherSharedFolder(f, true);
1360 public native void disableUploadArea() /*-{
1361 var uploader = $wnd.$("#uploader").pluploadQueue();
1362 var dropElm = $wnd.document.getElementById('rightPanel');
1363 $wnd.plupload.removeAllEvents(dropElm, uploader.id);
1366 public native void enableUploadArea() /*-{
1367 var uploader = $wnd.$("#uploader").pluploadQueue();
1368 if (uploader.runtime == 'html5') {
1369 uploader.settings.drop_element = 'rightPanel';
1370 var dropElm = $wnd.document.getElementById(uploader.settings.drop_element);
1371 uploader.trigger('PostInit');