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>();
273 public void onModuleLoad() {
274 if (parseUserCredentials())
278 private void initialize() {
279 VerticalPanel outer = new VerticalPanel();
280 outer.setWidth("100%");
282 topPanel = new TopPanel(this, Pithos.images);
283 topPanel.setWidth("100%");
286 messagePanel.setWidth("100%");
287 messagePanel.setVisible(false);
288 outer.add(messagePanel);
289 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
292 // Inner contains the various lists.
293 inner.sinkEvents(Event.ONCONTEXTMENU);
294 inner.setWidth("100%");
296 HorizontalPanel rightside = new HorizontalPanel();
297 rightside.addStyleName("pithos-rightSide");
298 rightside.setSpacing(5);
300 HTML folderStatistics = new HTML("5 Files (size: 1.1GB)");
301 folderStatistics.addStyleName("pithos-folderStatistics");
302 rightside.add(folderStatistics);
303 inner.add(rightside);
304 inner.setCellHorizontalAlignment(rightside, HasHorizontalAlignment.ALIGN_RIGHT);
305 inner.setCellVerticalAlignment(rightside, HasVerticalAlignment.ALIGN_MIDDLE);
306 inner.setCellHeight(rightside, "60px");
308 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
309 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
311 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
312 if (folderTreeSelectionModel.getSelectedObject() != null) {
313 deselectOthers(folderTreeView, folderTreeSelectionModel);
314 applyPermissions(folderTreeSelectionModel.getSelectedObject());
315 Folder f = folderTreeSelectionModel.getSelectedObject();
316 updateFolder(f, true, null);
320 selectionModels.add(folderTreeSelectionModel);
322 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
323 folderTreeView = new FolderTreeView(folderTreeViewModel);
325 fileList = new FileList(this, images, folderTreeView);
328 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
329 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
331 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
332 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
333 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
334 upload.setEnabled(false);
335 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
339 selectionModels.add(mysharedTreeSelectionModel);
340 mysharedTreeViewModel = new MysharedTreeViewModel(this, mysharedTreeSelectionModel);
341 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
343 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
344 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
346 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
347 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
348 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
349 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
350 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
354 selectionModels.add(otherSharedTreeSelectionModel);
355 otherSharedTreeViewModel = new OtherSharedTreeViewModel(this, otherSharedTreeSelectionModel);
356 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
358 groupTreeViewModel = new GroupTreeViewModel(this);
359 groupTreeView = new GroupTreeView(groupTreeViewModel);
361 VerticalPanel trees = new VerticalPanel();
363 upload = new Button("Upload File", new ClickHandler() {
365 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
366 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
369 upload.addStyleName("pithos-uploadButton");
372 HorizontalPanel treeHeader = new HorizontalPanel();
373 treeHeader.addStyleName("pithos-treeHeader");
374 treeHeader.add(new HTML("Total Files: 6 | Used: 2.1 of 50 GB (4.2%)"));
375 trees.add(treeHeader);
377 trees.add(folderTreeView);
378 trees.add(mysharedTreeView);
379 trees.add(otherSharedTreeView);
380 // trees.add(tagTreeView);
381 trees.add(groupTreeView);
382 // Add the left and right panels to the split panel.
383 splitPanel.setLeftWidget(trees);
384 splitPanel.setRightWidget(inner);
385 splitPanel.setSplitPosition("25%");
386 splitPanel.setSize("100%", "100%");
387 splitPanel.addStyleName("pithos-splitPanel");
388 outer.add(splitPanel);
390 statusPanel = new StatusPanel();
391 outer.add(statusPanel);
394 // Hook the window resize event, so that we can adjust the UI.
395 Window.addResizeHandler(this);
396 // Clear out the window's built-in margin, because we want to take
397 // advantage of the entire client area.
398 Window.setMargin("0px");
399 // Finally, add the outer panel to the RootPanel, so that it will be
401 RootPanel.get().add(outer);
402 // Call the window resized handler to get the initial sizes setup. Doing
403 // this in a deferred command causes it to occur after all widgets'
404 // sizes have been computed by the browser.
405 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
408 public void execute() {
409 onWindowResized(Window.getClientHeight());
413 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
415 public void execute() {
421 public void applyPermissions(Folder f) {
424 upload.setEnabled(false);
426 Boolean[] perms = f.getPermissions().get(username);
427 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
428 upload.setEnabled(true);
431 upload.setEnabled(false);
435 upload.setEnabled(false);
438 @SuppressWarnings({ "rawtypes", "unchecked" })
439 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
440 selectedTree = _selectedTree;
441 for (SingleSelectionModel s : selectionModels)
442 if (!s.equals(model))
443 s.setSelected(s.getSelectedObject(), false);
446 public void showFiles(Folder f) {
447 Set<File> files = f.getFiles();
451 public void showFiles(Set<File> files) {
452 //Iterator<File> iter = files.iterator();
453 //fetchFile(iter, files);
454 fileList.setFiles(new ArrayList<File>(files));
457 protected void fetchFile(final Iterator<File> iter, final Set<File> files) {
458 if (iter.hasNext()) {
459 File file = iter.next();
460 String path = file.getUri() + "?format=json";
461 GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
463 public void onSuccess(@SuppressWarnings("unused") File _result) {
464 fetchFile(iter, files);
468 public void onError(Throwable t) {
469 GWT.log("Error getting file", t);
470 if (t instanceof RestException)
471 displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
473 displayError("System error fetching file: " + t.getMessage());
476 getFile.setHeader("X-Auth-Token", "0000");
477 Scheduler.get().scheduleDeferred(getFile);
480 fileList.setFiles(new ArrayList<File>(files));
484 * Parse and store the user credentials to the appropriate fields.
486 private boolean parseUserCredentials() {
487 username = Window.Location.getParameter("user");
488 token = Window.Location.getParameter("token");
489 Configuration conf = (Configuration) GWT.create(Configuration.class);
490 if (username == null || username.length() == 0 || token == null || token.length() == 0) {
491 String cookie = conf.authCookie();
492 String auth = Cookies.getCookie(cookie);
497 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
498 if (authSplit.length != 2) {
502 username = authSplit[0];
503 token = authSplit[1];
506 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token);
511 * Redirect the user to the login page for authentication.
513 protected void authenticateUser() {
514 Configuration conf = (Configuration) GWT.create(Configuration.class);
515 Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
518 protected void fetchAccount() {
519 String path = "?format=json";
521 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
523 public void onSuccess(AccountResource _result) {
525 if (!account.hasHomeContainer())
526 createHomeContainer(account);
527 else if (!account.hasTrashContainer())
528 createTrashContainer();
530 for (Folder f : account.getContainers())
531 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
535 folderTreeViewModel.initialize(account);
536 groupTreeViewModel.initialize();
541 public void onError(Throwable t) {
542 GWT.log("Error getting account", t);
543 if (t instanceof RestException)
544 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
546 displayError("System error fetching user data: " + t.getMessage());
549 getAccount.setHeader("X-Auth-Token", token);
550 Scheduler.get().scheduleDeferred(getAccount);
553 protected void createHomeContainer(final AccountResource account) {
554 String path = "/" + Pithos.HOME_CONTAINER;
555 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
557 public void onSuccess(@SuppressWarnings("unused") Resource result) {
558 if (!account.hasTrashContainer())
559 createTrashContainer();
565 public void onError(Throwable t) {
566 GWT.log("Error creating pithos", t);
567 if (t instanceof RestException)
568 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
570 displayError("System error Error creating pithos: " + t.getMessage());
573 createPithos.setHeader("X-Auth-Token", getToken());
574 Scheduler.get().scheduleDeferred(createPithos);
577 protected void createTrashContainer() {
578 String path = "/" + Pithos.TRASH_CONTAINER;
579 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
581 public void onSuccess(@SuppressWarnings("unused") Resource result) {
586 public void onError(Throwable t) {
587 GWT.log("Error creating pithos", t);
588 if (t instanceof RestException)
589 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
591 displayError("System error Error creating pithos: " + t.getMessage());
594 createPithos.setHeader("X-Auth-Token", getToken());
595 Scheduler.get().scheduleDeferred(createPithos);
599 * Creates an HTML fragment that places an image & caption together, for use
602 * @param imageProto an image prototype for an image
603 * @param caption the group caption
604 * @return the header HTML fragment
606 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
607 String captionHTML = "<table class='caption' cellpadding='0' "
608 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
609 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
610 + caption + "</b></td></tr></table>";
614 protected void onWindowResized(int height) {
615 // Adjust the split panel to take up the available room in the window.
616 int newHeight = height - splitPanel.getAbsoluteTop() - 60;
619 splitPanel.setHeight("" + newHeight);
620 inner.setHeight("" + newHeight);
624 public void onResize(ResizeEvent event) {
625 int height = event.getHeight();
626 onWindowResized(height);
630 * Display an error message.
632 * @param msg the message to display
634 public void displayError(String msg) {
635 messagePanel.displayError(msg);
639 * Display a warning message.
641 * @param msg the message to display
643 public void displayWarning(String msg) {
644 messagePanel.displayWarning(msg);
648 * Display an informational message.
650 * @param msg the message to display
652 public void displayInformation(String msg) {
653 messagePanel.displayInformation(msg);
657 * Retrieve the fileList.
659 * @return the fileList
661 public FileList getFileList() {
666 * Retrieve the topPanel.
668 * @return the topPanel
670 TopPanel getTopPanel() {
675 * Retrieve the clipboard.
677 * @return the clipboard
679 public Clipboard getClipboard() {
683 public StatusPanel getStatusPanel() {
687 public String getToken() {
691 public String getWebDAVPassword() {
692 return webDAVPassword;
695 public static native void preventIESelection() /*-{
696 $doc.body.onselectstart = function () { return false; };
699 public static native void enableIESelection() /*-{
700 if ($doc.body.onselectstart != null)
701 $doc.body.onselectstart = null;
705 * @return the absolute path of the API root URL
707 public String getApiPath() {
708 Configuration conf = (Configuration) GWT.create(Configuration.class);
709 return conf.apiPath();
713 * History support for folder navigation
714 * adds a new browser history entry
718 public void updateHistory(String key){
719 // Replace any whitespace of the initial string to "+"
720 // String result = key.replaceAll("\\s","+");
721 // Add a new browser history entry.
722 // History.newItem(result);
723 History.newItem(key);
726 public void deleteFolder(final Folder folder) {
727 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + folder.getPrefix();
728 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
729 builder.setHeader("If-Modified-Since", "0");
730 builder.setHeader("X-Auth-Token", getToken());
732 builder.sendRequest("", new RequestCallback() {
734 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
735 if (response.getStatusCode() == Response.SC_OK) {
736 JSONValue json = JSONParser.parseStrict(response.getText());
737 JSONArray array = json.isArray();
740 deleteObject(folder, i, array);
746 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
747 displayError("System error unable to delete folder: " + exception.getMessage());
751 catch (RequestException e) {
755 public void deleteObject(final Folder folder, final int i, final JSONArray array) {
756 if (i < array.size()) {
757 JSONObject o = array.get(i).isObject();
758 if (o != null && !o.containsKey("subdir")) {
759 JSONString name = o.get("name").isString();
760 String path = "/" + folder.getContainer() + "/" + name.stringValue();
761 DeleteRequest delete = new DeleteRequest(getApiPath(), getUsername(), path) {
763 public void onSuccess(@SuppressWarnings("unused") Resource result) {
764 deleteObject(folder, i + 1, array);
768 public void onError(Throwable t) {
770 displayError("System error unable to delete folder: " + t.getMessage());
773 delete.setHeader("X-Auth-Token", getToken());
774 Scheduler.get().scheduleDeferred(delete);
776 else if (o != null) {
777 String subdir = o.get("subdir").isString().stringValue();
778 subdir = subdir.substring(0, subdir.length() - 1);
779 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + subdir;
780 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
781 builder.setHeader("If-Modified-Since", "0");
782 builder.setHeader("X-Auth-Token", getToken());
784 builder.sendRequest("", new RequestCallback() {
786 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
787 if (response.getStatusCode() == Response.SC_OK) {
788 JSONValue json = JSONParser.parseStrict(response.getText());
789 JSONArray array2 = json.isArray();
790 if (array2 != null) {
791 int l = array.size();
792 for (int j=0; j<array2.size(); j++) {
793 array.set(l++, array2.get(j));
796 deleteObject(folder, i + 1, array);
801 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
802 displayError("System error unable to delete folder: " + exception.getMessage());
806 catch (RequestException e) {
811 String path = folder.getUri();
812 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
814 public void onSuccess(@SuppressWarnings("unused") Resource result) {
815 updateFolder(folder.getParent(), true, null);
819 public void onError(Throwable t) {
821 if (t instanceof RestException) {
822 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
823 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
828 displayError("System error unable to delete folder: " + t.getMessage());
831 deleteFolder.setHeader("X-Auth-Token", getToken());
832 Scheduler.get().scheduleDeferred(deleteFolder);
836 public FolderTreeView getFolderTreeView() {
837 return folderTreeView;
840 public void copyFiles(final Iterator<File> iter, final String targetUri, final Command callback) {
841 if (iter.hasNext()) {
842 File file = iter.next();
843 String path = targetUri + "/" + file.getName();
844 PutRequest copyFile = new PutRequest(getApiPath(), getUsername(), path) {
846 public void onSuccess(@SuppressWarnings("unused") Resource result) {
847 copyFiles(iter, targetUri, callback);
851 public void onError(Throwable t) {
853 if (t instanceof RestException) {
854 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
857 displayError("System error unable to copy file: "+t.getMessage());
860 copyFile.setHeader("X-Auth-Token", getToken());
861 copyFile.setHeader("X-Copy-From", file.getUri());
862 Scheduler.get().scheduleDeferred(copyFile);
864 else if (callback != null) {
869 public void copySubfolders(final Iterator<Folder> iter, final String targetUri, final Command callback) {
870 if (iter.hasNext()) {
871 final Folder f = iter.next();
872 copyFolder(f, targetUri, callback);
874 else if (callback != null) {
879 public void copyFolder(final Folder f, final String targetUri, final Command callback) {
880 String path = targetUri + "/" + f.getName();
881 PutRequest createFolder = new PutRequest(getApiPath(), getUsername(), path) {
883 public void onSuccess(@SuppressWarnings("unused") Resource result) {
884 Iterator<File> iter = f.getFiles().iterator();
885 copyFiles(iter, targetUri + "/" + f.getName(), new Command() {
887 public void execute() {
888 Iterator<Folder> iterf = f.getSubfolders().iterator();
889 copySubfolders(iterf, targetUri + "/" + f.getName(), new Command() {
891 public void execute() {
900 public void onError(Throwable t) {
902 if (t instanceof RestException) {
903 displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
906 displayError("System error creating folder:" + t.getMessage());
909 createFolder.setHeader("X-Auth-Token", getToken());
910 createFolder.setHeader("Accept", "*/*");
911 createFolder.setHeader("Content-Length", "0");
912 createFolder.setHeader("Content-Type", "application/folder");
913 Scheduler.get().scheduleDeferred(createFolder);
916 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
917 selectionModels.add(model);
920 public OtherSharedTreeView getOtherSharedTreeView() {
921 return otherSharedTreeView;
924 public void updateTrash(boolean showFiles, Command callback) {
925 updateFolder(trash, showFiles, callback);
928 public void updateGroupsNode() {
929 groupTreeView.updateGroupNode(null);
932 public void addGroup(String groupname) {
933 Group newGroup = new Group(groupname);
934 account.addGroup(newGroup);
935 groupTreeView.updateGroupNode(null);
938 public void removeGroup(Group group) {
939 account.removeGroup(group);
943 public TreeView getSelectedTree() {
947 public Folder getSelection() {
948 return selectedTree.getSelection();