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>();
272 private HTML totalFiles;
274 private HTML usedBytes;
276 private HTML totalBytes;
278 private HTML usedPercent;
281 public void onModuleLoad() {
282 if (parseUserCredentials())
286 private void initialize() {
287 VerticalPanel outer = new VerticalPanel();
288 outer.setWidth("100%");
290 topPanel = new TopPanel(this, Pithos.images);
291 topPanel.setWidth("100%");
294 messagePanel.setWidth("100%");
295 messagePanel.setVisible(false);
296 outer.add(messagePanel);
297 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
300 // Inner contains the various lists.
301 inner.sinkEvents(Event.ONCONTEXTMENU);
302 inner.setWidth("100%");
304 HorizontalPanel rightside = new HorizontalPanel();
305 rightside.addStyleName("pithos-rightSide");
306 rightside.setSpacing(5);
308 HTML folderStatistics = new HTML("5 Files (size: 1.1GB)");
309 folderStatistics.addStyleName("pithos-folderStatistics");
310 rightside.add(folderStatistics);
311 inner.add(rightside);
312 inner.setCellHorizontalAlignment(rightside, HasHorizontalAlignment.ALIGN_RIGHT);
313 inner.setCellVerticalAlignment(rightside, HasVerticalAlignment.ALIGN_MIDDLE);
314 inner.setCellHeight(rightside, "60px");
316 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
317 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
319 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
320 if (folderTreeSelectionModel.getSelectedObject() != null) {
321 deselectOthers(folderTreeView, folderTreeSelectionModel);
322 applyPermissions(folderTreeSelectionModel.getSelectedObject());
323 Folder f = folderTreeSelectionModel.getSelectedObject();
324 updateFolder(f, true, null);
328 selectionModels.add(folderTreeSelectionModel);
330 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
331 folderTreeView = new FolderTreeView(folderTreeViewModel);
333 fileList = new FileList(this, images, folderTreeView);
336 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
337 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
339 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
340 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
341 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
342 upload.setEnabled(false);
343 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
347 selectionModels.add(mysharedTreeSelectionModel);
348 mysharedTreeViewModel = new MysharedTreeViewModel(this, mysharedTreeSelectionModel);
349 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
351 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
352 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
354 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
355 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
356 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
357 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
358 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
362 selectionModels.add(otherSharedTreeSelectionModel);
363 otherSharedTreeViewModel = new OtherSharedTreeViewModel(this, otherSharedTreeSelectionModel);
364 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
366 groupTreeViewModel = new GroupTreeViewModel(this);
367 groupTreeView = new GroupTreeView(groupTreeViewModel);
369 VerticalPanel trees = new VerticalPanel();
371 upload = new Button("Upload File", new ClickHandler() {
373 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
374 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
377 upload.addStyleName("pithos-uploadButton");
380 HorizontalPanel treeHeader = new HorizontalPanel();
381 treeHeader.addStyleName("pithos-treeHeader");
382 HorizontalPanel statistics = new HorizontalPanel();
383 statistics.add(new HTML("Total Files: "));
384 totalFiles = new HTML();
385 statistics.add(totalFiles);
386 statistics.add(new HTML(" | Used: "));
387 usedBytes = new HTML();
388 statistics.add(usedBytes);
389 statistics.add(new HTML(" of "));
390 totalBytes = new HTML();
391 statistics.add(totalBytes);
392 statistics.add(new HTML(" ("));
393 usedPercent = new HTML();
394 statistics.add(usedPercent);
395 statistics.add(new HTML("%)"));
396 treeHeader.add(statistics);
397 trees.add(treeHeader);
399 trees.add(folderTreeView);
400 trees.add(mysharedTreeView);
401 trees.add(otherSharedTreeView);
402 // trees.add(tagTreeView);
403 trees.add(groupTreeView);
404 // Add the left and right panels to the split panel.
405 splitPanel.setLeftWidget(trees);
406 splitPanel.setRightWidget(inner);
407 splitPanel.setSplitPosition("25%");
408 splitPanel.setSize("100%", "100%");
409 splitPanel.addStyleName("pithos-splitPanel");
410 outer.add(splitPanel);
412 statusPanel = new StatusPanel();
413 outer.add(statusPanel);
416 // Hook the window resize event, so that we can adjust the UI.
417 Window.addResizeHandler(this);
418 // Clear out the window's built-in margin, because we want to take
419 // advantage of the entire client area.
420 Window.setMargin("0px");
421 // Finally, add the outer panel to the RootPanel, so that it will be
423 RootPanel.get().add(outer);
424 // Call the window resized handler to get the initial sizes setup. Doing
425 // this in a deferred command causes it to occur after all widgets'
426 // sizes have been computed by the browser.
427 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
430 public void execute() {
431 onWindowResized(Window.getClientHeight());
435 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
437 public void execute() {
443 public void applyPermissions(Folder f) {
446 upload.setEnabled(false);
448 Boolean[] perms = f.getPermissions().get(username);
449 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
450 upload.setEnabled(true);
453 upload.setEnabled(false);
457 upload.setEnabled(false);
460 @SuppressWarnings({ "rawtypes", "unchecked" })
461 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
462 selectedTree = _selectedTree;
463 for (SingleSelectionModel s : selectionModels)
464 if (!s.equals(model))
465 s.setSelected(s.getSelectedObject(), false);
468 public void showFiles(Folder f) {
469 Set<File> files = f.getFiles();
473 public void showFiles(Set<File> files) {
474 //Iterator<File> iter = files.iterator();
475 //fetchFile(iter, files);
476 fileList.setFiles(new ArrayList<File>(files));
479 protected void fetchFile(final Iterator<File> iter, final Set<File> files) {
480 if (iter.hasNext()) {
481 File file = iter.next();
482 String path = file.getUri() + "?format=json";
483 GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
485 public void onSuccess(@SuppressWarnings("unused") File _result) {
486 fetchFile(iter, files);
490 public void onError(Throwable t) {
491 GWT.log("Error getting file", t);
492 if (t instanceof RestException)
493 displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
495 displayError("System error fetching file: " + t.getMessage());
498 getFile.setHeader("X-Auth-Token", "0000");
499 Scheduler.get().scheduleDeferred(getFile);
502 fileList.setFiles(new ArrayList<File>(files));
506 * Parse and store the user credentials to the appropriate fields.
508 private boolean parseUserCredentials() {
509 username = Window.Location.getParameter("user");
510 token = Window.Location.getParameter("token");
511 Configuration conf = (Configuration) GWT.create(Configuration.class);
512 if (username == null || username.length() == 0 || token == null || token.length() == 0) {
513 String cookie = conf.authCookie();
514 String auth = Cookies.getCookie(cookie);
519 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
520 if (authSplit.length != 2) {
524 username = authSplit[0];
525 token = authSplit[1];
528 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token);
533 * Redirect the user to the login page for authentication.
535 protected void authenticateUser() {
536 Configuration conf = (Configuration) GWT.create(Configuration.class);
537 Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
540 protected void fetchAccount() {
541 String path = "?format=json";
543 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
545 public void onSuccess(AccountResource _result) {
547 if (!account.hasHomeContainer())
548 createHomeContainer(account);
549 else if (!account.hasTrashContainer())
550 createTrashContainer();
552 for (Folder f : account.getContainers())
553 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
557 folderTreeViewModel.initialize(account);
558 groupTreeViewModel.initialize();
564 public void onError(Throwable t) {
565 GWT.log("Error getting account", t);
566 if (t instanceof RestException)
567 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
569 displayError("System error fetching user data: " + t.getMessage());
572 getAccount.setHeader("X-Auth-Token", token);
573 Scheduler.get().scheduleDeferred(getAccount);
576 protected void updateStatistics() {
577 totalFiles.setHTML(String.valueOf(account.getNumberOfObjects()));
578 usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
579 totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
580 usedPercent.setHTML(String.valueOf(account.getUsedPercentage()));
583 protected void createHomeContainer(final AccountResource account) {
584 String path = "/" + Pithos.HOME_CONTAINER;
585 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
587 public void onSuccess(@SuppressWarnings("unused") Resource result) {
588 if (!account.hasTrashContainer())
589 createTrashContainer();
595 public void onError(Throwable t) {
596 GWT.log("Error creating pithos", t);
597 if (t instanceof RestException)
598 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
600 displayError("System error Error creating pithos: " + t.getMessage());
603 createPithos.setHeader("X-Auth-Token", getToken());
604 Scheduler.get().scheduleDeferred(createPithos);
607 protected void createTrashContainer() {
608 String path = "/" + Pithos.TRASH_CONTAINER;
609 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
611 public void onSuccess(@SuppressWarnings("unused") Resource result) {
616 public void onError(Throwable t) {
617 GWT.log("Error creating pithos", t);
618 if (t instanceof RestException)
619 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
621 displayError("System error Error creating pithos: " + t.getMessage());
624 createPithos.setHeader("X-Auth-Token", getToken());
625 Scheduler.get().scheduleDeferred(createPithos);
629 * Creates an HTML fragment that places an image & caption together, for use
632 * @param imageProto an image prototype for an image
633 * @param caption the group caption
634 * @return the header HTML fragment
636 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
637 String captionHTML = "<table class='caption' cellpadding='0' "
638 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
639 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
640 + caption + "</b></td></tr></table>";
644 protected void onWindowResized(int height) {
645 // Adjust the split panel to take up the available room in the window.
646 int newHeight = height - splitPanel.getAbsoluteTop() - 60;
649 splitPanel.setHeight("" + newHeight);
650 inner.setHeight("" + newHeight);
654 public void onResize(ResizeEvent event) {
655 int height = event.getHeight();
656 onWindowResized(height);
660 * Display an error message.
662 * @param msg the message to display
664 public void displayError(String msg) {
665 messagePanel.displayError(msg);
669 * Display a warning message.
671 * @param msg the message to display
673 public void displayWarning(String msg) {
674 messagePanel.displayWarning(msg);
678 * Display an informational message.
680 * @param msg the message to display
682 public void displayInformation(String msg) {
683 messagePanel.displayInformation(msg);
687 * Retrieve the fileList.
689 * @return the fileList
691 public FileList getFileList() {
696 * Retrieve the topPanel.
698 * @return the topPanel
700 TopPanel getTopPanel() {
705 * Retrieve the clipboard.
707 * @return the clipboard
709 public Clipboard getClipboard() {
713 public StatusPanel getStatusPanel() {
717 public String getToken() {
721 public String getWebDAVPassword() {
722 return webDAVPassword;
725 public static native void preventIESelection() /*-{
726 $doc.body.onselectstart = function () { return false; };
729 public static native void enableIESelection() /*-{
730 if ($doc.body.onselectstart != null)
731 $doc.body.onselectstart = null;
735 * @return the absolute path of the API root URL
737 public String getApiPath() {
738 Configuration conf = (Configuration) GWT.create(Configuration.class);
739 return conf.apiPath();
743 * History support for folder navigation
744 * adds a new browser history entry
748 public void updateHistory(String key){
749 // Replace any whitespace of the initial string to "+"
750 // String result = key.replaceAll("\\s","+");
751 // Add a new browser history entry.
752 // History.newItem(result);
753 History.newItem(key);
756 public void deleteFolder(final Folder folder) {
757 String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + folder.getPrefix();
758 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
759 builder.setHeader("If-Modified-Since", "0");
760 builder.setHeader("X-Auth-Token", getToken());
762 builder.sendRequest("", new RequestCallback() {
764 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
765 if (response.getStatusCode() == Response.SC_OK) {
766 JSONValue json = JSONParser.parseStrict(response.getText());
767 JSONArray array = json.isArray();
770 deleteObject(folder, i, array);
776 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
777 displayError("System error unable to delete folder: " + exception.getMessage());
781 catch (RequestException e) {
785 public void deleteObject(final Folder folder, final int i, final JSONArray array) {
786 if (i < array.size()) {
787 JSONObject o = array.get(i).isObject();
788 if (o != null && !o.containsKey("subdir")) {
789 JSONString name = o.get("name").isString();
790 String path = "/" + folder.getContainer() + "/" + name.stringValue();
791 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), path) {
793 public void onSuccess(@SuppressWarnings("unused") Resource result) {
794 deleteObject(folder, i + 1, array);
798 public void onError(Throwable t) {
800 displayError("System error unable to delete folder: " + t.getMessage());
803 delete.setHeader("X-Auth-Token", getToken());
804 Scheduler.get().scheduleDeferred(delete);
806 else if (o != null) {
807 String subdir = o.get("subdir").isString().stringValue();
808 subdir = subdir.substring(0, subdir.length() - 1);
809 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + subdir;
810 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
811 builder.setHeader("If-Modified-Since", "0");
812 builder.setHeader("X-Auth-Token", getToken());
814 builder.sendRequest("", new RequestCallback() {
816 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
817 if (response.getStatusCode() == Response.SC_OK) {
818 JSONValue json = JSONParser.parseStrict(response.getText());
819 JSONArray array2 = json.isArray();
820 if (array2 != null) {
821 int l = array.size();
822 for (int j=0; j<array2.size(); j++) {
823 array.set(l++, array2.get(j));
826 deleteObject(folder, i + 1, array);
831 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
832 displayError("System error unable to delete folder: " + exception.getMessage());
836 catch (RequestException e) {
841 String path = folder.getUri();
842 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
844 public void onSuccess(@SuppressWarnings("unused") Resource result) {
845 updateFolder(folder.getParent(), true, null);
849 public void onError(Throwable t) {
851 if (t instanceof RestException) {
852 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
853 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
858 displayError("System error unable to delete folder: " + t.getMessage());
861 deleteFolder.setHeader("X-Auth-Token", getToken());
862 Scheduler.get().scheduleDeferred(deleteFolder);
866 public FolderTreeView getFolderTreeView() {
867 return folderTreeView;
870 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
871 if (iter.hasNext()) {
872 File file = iter.next();
873 String path = targetUri + "/" + file.getName();
874 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
876 public void onSuccess(@SuppressWarnings("unused") Resource result) {
877 copyFiles(iter, targetUsername, targetUri, callback);
881 public void onError(Throwable t) {
883 if (t instanceof RestException) {
884 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
887 displayError("System error unable to copy file: "+t.getMessage());
890 copyFile.setHeader("X-Auth-Token", getToken());
891 copyFile.setHeader("X-Copy-From", file.getUri());
892 Scheduler.get().scheduleDeferred(copyFile);
894 else if (callback != null) {
899 public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
900 if (iter.hasNext()) {
901 final Folder f = iter.next();
902 copyFolder(f, targetUsername, targetUri, callback);
904 else if (callback != null) {
909 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
910 String path = targetUri + "/" + f.getName();
911 PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
913 public void onSuccess(@SuppressWarnings("unused") Resource result) {
914 Iterator<File> iter = f.getFiles().iterator();
915 copyFiles(iter, targetUsername, targetUri + "/" + f.getName(), new Command() {
917 public void execute() {
918 Iterator<Folder> iterf = f.getSubfolders().iterator();
919 copySubfolders(iterf, targetUsername, targetUri + "/" + f.getName(), new Command() {
921 public void execute() {
930 public void onError(Throwable t) {
932 if (t instanceof RestException) {
933 displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
936 displayError("System error creating folder:" + t.getMessage());
939 createFolder.setHeader("X-Auth-Token", getToken());
940 createFolder.setHeader("Accept", "*/*");
941 createFolder.setHeader("Content-Length", "0");
942 createFolder.setHeader("Content-Type", "application/folder");
943 Scheduler.get().scheduleDeferred(createFolder);
946 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
947 selectionModels.add(model);
950 public OtherSharedTreeView getOtherSharedTreeView() {
951 return otherSharedTreeView;
954 public void updateTrash(boolean showFiles, Command callback) {
955 updateFolder(trash, showFiles, callback);
958 public void updateGroupsNode() {
959 groupTreeView.updateGroupNode(null);
962 public void addGroup(String groupname) {
963 Group newGroup = new Group(groupname);
964 account.addGroup(newGroup);
965 groupTreeView.updateGroupNode(null);
968 public void removeGroup(Group group) {
969 account.removeGroup(group);
973 public TreeView getSelectedTree() {
977 public Folder getSelection() {
978 return selectedTree.getSelection();