2 * Copyright 2011 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.commands.UploadFileCommand;
38 import gr.grnet.pithos.web.client.foldertree.AccountResource;
39 import gr.grnet.pithos.web.client.foldertree.File;
40 import gr.grnet.pithos.web.client.foldertree.Folder;
41 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
42 import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
43 import gr.grnet.pithos.web.client.foldertree.Resource;
44 import gr.grnet.pithos.web.client.grouptree.Group;
45 import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
46 import gr.grnet.pithos.web.client.grouptree.GroupTreeViewModel;
47 import gr.grnet.pithos.web.client.grouptree.User;
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.PutRequest;
55 import gr.grnet.pithos.web.client.rest.RestException;
56 import gr.grnet.pithos.web.client.tagtree.Tag;
57 import gr.grnet.pithos.web.client.tagtree.TagTreeView;
58 import gr.grnet.pithos.web.client.tagtree.TagTreeViewModel;
60 import java.util.ArrayList;
61 import java.util.HashMap;
62 import java.util.Iterator;
63 import java.util.List;
66 import com.google.gwt.core.client.EntryPoint;
67 import com.google.gwt.core.client.GWT;
68 import com.google.gwt.core.client.Scheduler;
69 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
70 import com.google.gwt.event.dom.client.ClickEvent;
71 import com.google.gwt.event.dom.client.ClickHandler;
72 import com.google.gwt.event.logical.shared.ResizeEvent;
73 import com.google.gwt.event.logical.shared.ResizeHandler;
74 import com.google.gwt.http.client.Request;
75 import com.google.gwt.http.client.RequestBuilder;
76 import com.google.gwt.http.client.RequestCallback;
77 import com.google.gwt.http.client.RequestException;
78 import com.google.gwt.http.client.Response;
79 import com.google.gwt.json.client.JSONArray;
80 import com.google.gwt.json.client.JSONObject;
81 import com.google.gwt.json.client.JSONParser;
82 import com.google.gwt.json.client.JSONString;
83 import com.google.gwt.json.client.JSONValue;
84 import com.google.gwt.resources.client.ImageResource;
85 import com.google.gwt.user.client.Command;
86 import com.google.gwt.user.client.Cookies;
87 import com.google.gwt.user.client.Event;
88 import com.google.gwt.user.client.History;
89 import com.google.gwt.user.client.Window;
90 import com.google.gwt.user.client.ui.AbstractImagePrototype;
91 import com.google.gwt.user.client.ui.Button;
92 import com.google.gwt.user.client.ui.HTML;
93 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
94 import com.google.gwt.user.client.ui.HasVerticalAlignment;
95 import com.google.gwt.user.client.ui.HorizontalPanel;
96 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
97 import com.google.gwt.user.client.ui.Image;
98 import com.google.gwt.user.client.ui.PushButton;
99 import com.google.gwt.user.client.ui.RootPanel;
100 import com.google.gwt.user.client.ui.VerticalPanel;
101 import com.google.gwt.view.client.SelectionChangeEvent;
102 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
103 import com.google.gwt.view.client.SingleSelectionModel;
106 * Entry point classes define <code>onModuleLoad()</code>.
108 public class Pithos implements EntryPoint, ResizeHandler {
110 public static final String HOME_CONTAINER = "pithos";
112 public static final String TRASH_CONTAINER = "trash";
115 * Instantiate an application-level image bundle. This object will provide
116 * programmatic access to all the images needed by widgets.
118 private static Images images = (Images) GWT.create(Images.class);
120 public String getUsername() {
124 public void setAccount(AccountResource acct) {
128 public AccountResource getAccount() {
132 public void updateFolder(Folder f, boolean showfiles, Command callback) {
133 folderTreeView.updateFolder(f, showfiles, callback);
136 public void updateGroupNode(Group group) {
137 groupTreeView.updateGroupNode(group);
140 public void updateSharedFolder(Folder f, boolean showfiles) {
141 mysharedTreeView.updateFolder(f, showfiles);
144 public void updateOtherSharedFolder(Folder f, boolean showfiles) {
145 otherSharedTreeView.updateFolder(f, showfiles);
148 public void updateTag(Tag t) {
149 tagTreeView.updateTag(t);
152 public void updateTags() {
153 tagTreeViewModel.initialize(getAllTags());
156 public List<Tag> getAllTags() {
157 List<Tag> tagList = new ArrayList<Tag>();
158 for (Folder f : account.getContainers()) {
159 for (String t : f.getTags()) {
160 tagList.add(new Tag(t));
166 public MysharedTreeView getMySharedTreeView() {
167 return mysharedTreeView;
171 * An aggregate image bundle that pulls together all the images for this
172 * application into a single bundle.
174 public interface Images extends TopPanel.Images, FileList.Images {
176 @Source("gr/grnet/pithos/resources/document.png")
177 ImageResource folders();
179 @Source("gr/grnet/pithos/resources/edit_group_22.png")
180 ImageResource groups();
182 @Source("gr/grnet/pithos/resources/search.png")
183 ImageResource search();
187 * The Application Clipboard implementation;
189 private Clipboard clipboard = new Clipboard();
192 * The top panel that contains the menu bar.
194 private TopPanel topPanel;
197 * The panel that contains the various system messages.
199 private MessagePanel messagePanel = new MessagePanel(Pithos.images);
202 * The bottom panel that contains the status bar.
204 private StatusPanel statusPanel = null;
207 * The file list widget.
209 private FileList fileList;
212 * The tab panel that occupies the right side of the screen.
214 private VerticalPanel inner = new VerticalPanel();
218 * The split panel that will contain the left and right panels.
220 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
223 * The currently selected item in the application, for use by the Edit menu
224 * commands. Potential types are Folder, File, User and Group.
226 private Object currentSelection;
230 * The WebDAV password of the current user
232 private String webDAVPassword;
234 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
236 private String username = null;
239 * The authentication token of the current user.
241 private String token;
243 SingleSelectionModel<Folder> folderTreeSelectionModel;
244 FolderTreeViewModel folderTreeViewModel;
245 FolderTreeView folderTreeView;
247 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
248 private MysharedTreeViewModel mysharedTreeViewModel;
249 MysharedTreeView mysharedTreeView;
251 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
252 private OtherSharedTreeViewModel otherSharedTreeViewModel;
253 OtherSharedTreeView otherSharedTreeView;
255 protected SingleSelectionModel<Tag> tagTreeSelectionModel;
256 private TagTreeViewModel tagTreeViewModel;
257 private TagTreeView tagTreeView;
259 private GroupTreeViewModel groupTreeViewModel;
260 private GroupTreeView groupTreeView;
262 private TreeView selectedTree;
263 protected AccountResource account;
265 private Folder trash;
267 @SuppressWarnings("rawtypes")
268 private List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
271 public void onModuleLoad() {
272 if (parseUserCredentials())
276 private void initialize() {
277 VerticalPanel outer = new VerticalPanel();
278 outer.setWidth("100%");
280 topPanel = new TopPanel(this, Pithos.images);
281 topPanel.setWidth("100%");
284 messagePanel.setWidth("100%");
285 messagePanel.setVisible(false);
286 outer.add(messagePanel);
287 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
290 // Inner contains the various lists.
291 inner.sinkEvents(Event.ONCONTEXTMENU);
292 inner.setWidth("100%");
294 HorizontalPanel rightside = new HorizontalPanel();
295 rightside.addStyleName("pithos-rightSide");
296 rightside.setSpacing(5);
298 PushButton parentButton = new PushButton(new Image(images.asc()), new ClickHandler() {
300 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
304 parentButton.addStyleName("pithos-parentButton");
305 rightside.add(parentButton);
307 HTML folderStatistics = new HTML("5 Files (size: 1.1GB)");
308 folderStatistics.addStyleName("pithos-folderStatistics");
309 rightside.add(folderStatistics);
310 inner.add(rightside);
311 inner.setCellHorizontalAlignment(rightside, HasHorizontalAlignment.ALIGN_RIGHT);
312 inner.setCellVerticalAlignment(rightside, HasVerticalAlignment.ALIGN_MIDDLE);
313 inner.setCellHeight(rightside, "60px");
315 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
316 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
318 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
319 if (folderTreeSelectionModel.getSelectedObject() != null) {
320 deselectOthers(folderTreeView, folderTreeSelectionModel);
321 Folder f = folderTreeSelectionModel.getSelectedObject();
322 updateFolder(f, true, null);
326 selectionModels.add(folderTreeSelectionModel);
328 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
329 folderTreeView = new FolderTreeView(folderTreeViewModel);
331 fileList = new FileList(this, images, folderTreeView);
334 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
335 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
337 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
338 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
339 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
340 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
344 selectionModels.add(mysharedTreeSelectionModel);
345 mysharedTreeViewModel = new MysharedTreeViewModel(this, mysharedTreeSelectionModel);
346 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
348 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
349 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
351 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
352 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
353 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
354 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
358 selectionModels.add(otherSharedTreeSelectionModel);
359 otherSharedTreeViewModel = new OtherSharedTreeViewModel(this, otherSharedTreeSelectionModel);
360 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
362 groupTreeViewModel = new GroupTreeViewModel(this);
363 groupTreeView = new GroupTreeView(groupTreeViewModel);
365 VerticalPanel trees = new VerticalPanel();
367 Button upload = new Button("Upload File", new ClickHandler() {
369 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
370 new UploadFileCommand(Pithos.this, null, folderTreeView.getSelection()).execute();
373 upload.addStyleName("pithos-uploadButton");
376 HorizontalPanel treeHeader = new HorizontalPanel();
377 treeHeader.addStyleName("pithos-treeHeader");
378 treeHeader.add(new HTML("Total Files: 6 | Used: 2.1 of 50 GB (4.2%)"));
379 trees.add(treeHeader);
381 trees.add(folderTreeView);
382 trees.add(mysharedTreeView);
383 trees.add(otherSharedTreeView);
384 // trees.add(tagTreeView);
385 trees.add(groupTreeView);
386 // Add the left and right panels to the split panel.
387 splitPanel.setLeftWidget(trees);
388 splitPanel.setRightWidget(inner);
389 splitPanel.setSplitPosition("25%");
390 splitPanel.setSize("100%", "100%");
391 splitPanel.addStyleName("pithos-splitPanel");
392 outer.add(splitPanel);
394 statusPanel = new StatusPanel();
395 outer.add(statusPanel);
398 // Hook the window resize event, so that we can adjust the UI.
399 Window.addResizeHandler(this);
400 // Clear out the window's built-in margin, because we want to take
401 // advantage of the entire client area.
402 Window.setMargin("0px");
403 // Finally, add the outer panel to the RootPanel, so that it will be
405 RootPanel.get().add(outer);
406 // Call the window resized handler to get the initial sizes setup. Doing
407 // this in a deferred command causes it to occur after all widgets'
408 // sizes have been computed by the browser.
409 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
412 public void execute() {
413 onWindowResized(Window.getClientHeight());
417 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
419 public void execute() {
425 @SuppressWarnings({ "rawtypes", "unchecked" })
426 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
427 selectedTree = _selectedTree;
428 for (SingleSelectionModel s : selectionModels)
429 if (!s.equals(model))
430 s.setSelected(s.getSelectedObject(), false);
433 public void showFiles(Folder f) {
434 Set<File> files = f.getFiles();
438 public void showFiles(Set<File> files) {
439 //Iterator<File> iter = files.iterator();
440 //fetchFile(iter, files);
441 fileList.setFiles(new ArrayList<File>(files));
444 protected void fetchFile(final Iterator<File> iter, final Set<File> files) {
445 if (iter.hasNext()) {
446 File file = iter.next();
447 String path = file.getUri() + "?format=json";
448 GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
450 public void onSuccess(@SuppressWarnings("unused") File _result) {
451 fetchFile(iter, files);
455 public void onError(Throwable t) {
456 GWT.log("Error getting file", t);
457 if (t instanceof RestException)
458 displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
460 displayError("System error fetching file: " + t.getMessage());
463 getFile.setHeader("X-Auth-Token", "0000");
464 Scheduler.get().scheduleDeferred(getFile);
467 fileList.setFiles(new ArrayList<File>(files));
471 * Parse and store the user credentials to the appropriate fields.
473 private boolean parseUserCredentials() {
474 username = Window.Location.getParameter("user");
475 token = Window.Location.getParameter("token");
476 Configuration conf = (Configuration) GWT.create(Configuration.class);
477 if (username == null || username.length() == 0 || token == null || token.length() == 0) {
478 String cookie = conf.authCookie();
479 String auth = Cookies.getCookie(cookie);
484 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
485 if (authSplit.length != 2) {
489 username = authSplit[0];
490 token = authSplit[1];
493 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token);
498 * Redirect the user to the login page for authentication.
500 protected void authenticateUser() {
501 Configuration conf = (Configuration) GWT.create(Configuration.class);
502 Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
505 protected void fetchAccount() {
506 String path = "?format=json";
508 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
510 public void onSuccess(AccountResource _result) {
512 if (!account.hasHomeContainer())
513 createHomeContainer(account);
514 else if (!account.hasTrashContainer())
515 createTrashContainer();
517 for (Folder f : account.getContainers())
518 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
522 folderTreeViewModel.initialize(account);
523 groupTreeViewModel.initialize();
528 public void onError(Throwable t) {
529 GWT.log("Error getting account", t);
530 if (t instanceof RestException)
531 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
533 displayError("System error fetching user data: " + t.getMessage());
536 getAccount.setHeader("X-Auth-Token", token);
537 Scheduler.get().scheduleDeferred(getAccount);
540 protected void createHomeContainer(final AccountResource account) {
541 String path = "/" + Pithos.HOME_CONTAINER;
542 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
544 public void onSuccess(@SuppressWarnings("unused") Resource result) {
545 if (!account.hasTrashContainer())
546 createTrashContainer();
552 public void onError(Throwable t) {
553 GWT.log("Error creating pithos", t);
554 if (t instanceof RestException)
555 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
557 displayError("System error Error creating pithos: " + t.getMessage());
560 createPithos.setHeader("X-Auth-Token", getToken());
561 Scheduler.get().scheduleDeferred(createPithos);
564 protected void createTrashContainer() {
565 String path = "/" + Pithos.TRASH_CONTAINER;
566 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
568 public void onSuccess(@SuppressWarnings("unused") Resource result) {
573 public void onError(Throwable t) {
574 GWT.log("Error creating pithos", t);
575 if (t instanceof RestException)
576 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
578 displayError("System error Error creating pithos: " + t.getMessage());
581 createPithos.setHeader("X-Auth-Token", getToken());
582 Scheduler.get().scheduleDeferred(createPithos);
586 * Creates an HTML fragment that places an image & caption together, for use
589 * @param imageProto an image prototype for an image
590 * @param caption the group caption
591 * @return the header HTML fragment
593 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
594 String captionHTML = "<table class='caption' cellpadding='0' "
595 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
596 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
597 + caption + "</b></td></tr></table>";
601 protected void onWindowResized(int height) {
602 // Adjust the split panel to take up the available room in the window.
603 int newHeight = height - splitPanel.getAbsoluteTop() - 60;
606 splitPanel.setHeight("" + newHeight);
607 inner.setHeight("" + newHeight);
611 public void onResize(ResizeEvent event) {
612 int height = event.getHeight();
613 onWindowResized(height);
617 * Display an error message.
619 * @param msg the message to display
621 public void displayError(String msg) {
622 messagePanel.displayError(msg);
626 * Display a warning message.
628 * @param msg the message to display
630 public void displayWarning(String msg) {
631 messagePanel.displayWarning(msg);
635 * Display an informational message.
637 * @param msg the message to display
639 public void displayInformation(String msg) {
640 messagePanel.displayInformation(msg);
644 * Retrieve the fileList.
646 * @return the fileList
648 public FileList getFileList() {
653 * Retrieve the topPanel.
655 * @return the topPanel
657 TopPanel getTopPanel() {
662 * Retrieve the clipboard.
664 * @return the clipboard
666 public Clipboard getClipboard() {
670 public StatusPanel getStatusPanel() {
674 public String getToken() {
678 public String getWebDAVPassword() {
679 return webDAVPassword;
682 public static native void preventIESelection() /*-{
683 $doc.body.onselectstart = function () { return false; };
686 public static native void enableIESelection() /*-{
687 if ($doc.body.onselectstart != null)
688 $doc.body.onselectstart = null;
692 * @return the absolute path of the API root URL
694 public String getApiPath() {
695 Configuration conf = (Configuration) GWT.create(Configuration.class);
696 return conf.apiPath();
700 * History support for folder navigation
701 * adds a new browser history entry
705 public void updateHistory(String key){
706 // Replace any whitespace of the initial string to "+"
707 // String result = key.replaceAll("\\s","+");
708 // Add a new browser history entry.
709 // History.newItem(result);
710 History.newItem(key);
713 public void deleteFolder(final Folder folder) {
714 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + folder.getPrefix();
715 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
716 builder.setHeader("If-Modified-Since", "0");
717 builder.setHeader("X-Auth-Token", getToken());
719 builder.sendRequest("", new RequestCallback() {
721 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
722 if (response.getStatusCode() == Response.SC_OK) {
723 JSONValue json = JSONParser.parseStrict(response.getText());
724 JSONArray array = json.isArray();
727 deleteObject(folder, i, array);
733 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
734 displayError("System error unable to delete folder: " + exception.getMessage());
738 catch (RequestException e) {
742 public void deleteObject(final Folder folder, final int i, final JSONArray array) {
743 if (i < array.size()) {
744 JSONObject o = array.get(i).isObject();
745 if (o != null && !o.containsKey("subdir")) {
746 JSONString name = o.get("name").isString();
747 String path = "/" + folder.getContainer() + "/" + name.stringValue();
748 DeleteRequest delete = new DeleteRequest(getApiPath(), getUsername(), path) {
750 public void onSuccess(@SuppressWarnings("unused") Resource result) {
751 deleteObject(folder, i + 1, array);
755 public void onError(Throwable t) {
757 displayError("System error unable to delete folder: " + t.getMessage());
760 delete.setHeader("X-Auth-Token", getToken());
761 Scheduler.get().scheduleDeferred(delete);
763 else if (o != null) {
764 String subdir = o.get("subdir").isString().stringValue();
765 subdir = subdir.substring(0, subdir.length() - 1);
766 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + subdir;
767 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
768 builder.setHeader("If-Modified-Since", "0");
769 builder.setHeader("X-Auth-Token", getToken());
771 builder.sendRequest("", new RequestCallback() {
773 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
774 if (response.getStatusCode() == Response.SC_OK) {
775 JSONValue json = JSONParser.parseStrict(response.getText());
776 JSONArray array2 = json.isArray();
777 if (array2 != null) {
778 int l = array.size();
779 for (int j=0; j<array2.size(); j++) {
780 array.set(l++, array2.get(j));
783 deleteObject(folder, i + 1, array);
788 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
789 displayError("System error unable to delete folder: " + exception.getMessage());
793 catch (RequestException e) {
798 String path = folder.getUri();
799 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
801 public void onSuccess(@SuppressWarnings("unused") Resource result) {
802 updateFolder(folder.getParent(), true, null);
806 public void onError(Throwable t) {
808 if (t instanceof RestException) {
809 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
810 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
815 displayError("System error unable to delete folder: " + t.getMessage());
818 deleteFolder.setHeader("X-Auth-Token", getToken());
819 Scheduler.get().scheduleDeferred(deleteFolder);
823 public FolderTreeView getFolderTreeView() {
824 return folderTreeView;
827 public void copyFiles(final Iterator<File> iter, final String targetUri, final Command callback) {
828 if (iter.hasNext()) {
829 File file = iter.next();
830 String path = targetUri + "/" + file.getName();
831 PutRequest copyFile = new PutRequest(getApiPath(), getUsername(), path) {
833 public void onSuccess(@SuppressWarnings("unused") Resource result) {
834 copyFiles(iter, targetUri, callback);
838 public void onError(Throwable t) {
840 if (t instanceof RestException) {
841 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
844 displayError("System error unable to copy file: "+t.getMessage());
847 copyFile.setHeader("X-Auth-Token", getToken());
848 copyFile.setHeader("X-Copy-From", file.getUri());
849 Scheduler.get().scheduleDeferred(copyFile);
851 else if (callback != null) {
856 public void copySubfolders(final Iterator<Folder> iter, final String targetUri, final Command callback) {
857 if (iter.hasNext()) {
858 final Folder f = iter.next();
859 copyFolder(f, targetUri, callback);
861 else if (callback != null) {
866 public void copyFolder(final Folder f, final String targetUri, final Command callback) {
867 String path = targetUri + "/" + f.getName();
868 PutRequest createFolder = new PutRequest(getApiPath(), getUsername(), path) {
870 public void onSuccess(@SuppressWarnings("unused") Resource result) {
871 Iterator<File> iter = f.getFiles().iterator();
872 copyFiles(iter, targetUri + "/" + f.getName(), new Command() {
874 public void execute() {
875 Iterator<Folder> iterf = f.getSubfolders().iterator();
876 copySubfolders(iterf, targetUri + "/" + f.getName(), new Command() {
878 public void execute() {
887 public void onError(Throwable t) {
889 if (t instanceof RestException) {
890 displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
893 displayError("System error creating folder:" + t.getMessage());
896 createFolder.setHeader("X-Auth-Token", getToken());
897 createFolder.setHeader("Accept", "*/*");
898 createFolder.setHeader("Content-Length", "0");
899 createFolder.setHeader("Content-Type", "application/folder");
900 Scheduler.get().scheduleDeferred(createFolder);
903 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
904 selectionModels.add(model);
907 public OtherSharedTreeView getOtherSharedTreeView() {
908 return otherSharedTreeView;
911 public void updateTrash(boolean showFiles, Command callback) {
912 updateFolder(trash, showFiles, callback);
915 public void updateGroupsNode() {
916 groupTreeView.updateGroupNode(null);
919 public void addGroup(String groupname) {
920 Group newGroup = new Group(groupname);
921 account.addGroup(newGroup);
922 groupTreeView.updateGroupNode(null);
925 public void removeGroup(Group group) {
926 account.removeGroup(group);
930 public TreeView getSelectedTree() {
934 public Folder getSelection() {
935 return selectedTree.getSelection();