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.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.mysharedtree.MysharedTreeView;
48 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
49 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
50 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
51 import gr.grnet.pithos.web.client.rest.DeleteRequest;
52 import gr.grnet.pithos.web.client.rest.GetRequest;
53 import gr.grnet.pithos.web.client.rest.HeadRequest;
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;
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.ScheduledCommand;
68 import com.google.gwt.event.dom.client.ClickEvent;
69 import com.google.gwt.event.dom.client.ClickHandler;
70 import com.google.gwt.event.logical.shared.ResizeEvent;
71 import com.google.gwt.event.logical.shared.ResizeHandler;
72 import com.google.gwt.http.client.Request;
73 import com.google.gwt.http.client.RequestBuilder;
74 import com.google.gwt.http.client.RequestCallback;
75 import com.google.gwt.http.client.RequestException;
76 import com.google.gwt.http.client.Response;
77 import com.google.gwt.http.client.URL;
78 import com.google.gwt.i18n.client.NumberFormat;
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.resources.client.ImageResource.ImageOptions;
86 import com.google.gwt.user.client.Command;
87 import com.google.gwt.user.client.Cookies;
88 import com.google.gwt.user.client.Event;
89 import com.google.gwt.user.client.History;
90 import com.google.gwt.user.client.Window;
91 import com.google.gwt.user.client.ui.AbstractImagePrototype;
92 import com.google.gwt.user.client.ui.Button;
93 import com.google.gwt.user.client.ui.Composite;
94 import com.google.gwt.user.client.ui.HTML;
95 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
96 import com.google.gwt.user.client.ui.HasVerticalAlignment;
97 import com.google.gwt.user.client.ui.HorizontalPanel;
98 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
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";
114 public static final Configuration config = GWT.create(Configuration.class);
117 * Instantiate an application-level image bundle. This object will provide
118 * programmatic access to all the images needed by widgets.
120 static Images images = (Images) GWT.create(Images.class);
122 public String getUsername() {
126 public void setAccount(AccountResource acct) {
130 public AccountResource getAccount() {
134 public void updateFolder(Folder f, boolean showfiles, Command callback) {
135 folderTreeView.updateFolder(f, showfiles, callback);
138 public void updateGroupNode(Group group) {
139 groupTreeView.updateGroupNode(group);
142 public void updateMySharedRoot() {
143 mysharedTreeView.updateRoot();
146 public void updateSharedFolder(Folder f, boolean showfiles) {
147 mysharedTreeView.updateFolder(f, showfiles);
150 public void updateOtherSharedFolder(Folder f, boolean showfiles) {
151 otherSharedTreeView.updateFolder(f, showfiles);
154 public MysharedTreeView getMySharedTreeView() {
155 return mysharedTreeView;
159 * An aggregate image bundle that pulls together all the images for this
160 * application into a single bundle.
162 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
164 @Source("gr/grnet/pithos/resources/document.png")
165 ImageResource folders();
167 @Source("gr/grnet/pithos/resources/advancedsettings.png")
168 @ImageOptions(width=32, height=32)
169 ImageResource tools();
172 private Throwable error;
175 * The Application Clipboard implementation;
177 private Clipboard clipboard = new Clipboard();
180 * The top panel that contains the menu bar.
182 private TopPanel topPanel;
185 * The panel that contains the various system messages.
187 private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
190 * The bottom panel that contains the status bar.
192 private StatusPanel statusPanel = null;
195 * The file list widget.
197 private FileList fileList;
200 * The tab panel that occupies the right side of the screen.
202 private VerticalPanel inner = new VerticalPanel();
206 * The split panel that will contain the left and right panels.
208 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
211 * The currently selected item in the application, for use by the Edit menu
212 * commands. Potential types are Folder, File, User and Group.
214 private Object currentSelection;
216 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
218 private String username = null;
221 * The authentication token of the current user.
223 private String token;
227 SingleSelectionModel<Folder> folderTreeSelectionModel;
228 FolderTreeViewModel folderTreeViewModel;
229 FolderTreeView folderTreeView;
231 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
232 MysharedTreeViewModel mysharedTreeViewModel;
233 MysharedTreeView mysharedTreeView = null;;
235 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
236 OtherSharedTreeViewModel otherSharedTreeViewModel;
237 OtherSharedTreeView otherSharedTreeView = null;
239 GroupTreeViewModel groupTreeViewModel;
240 private GroupTreeView groupTreeView;
242 TreeView selectedTree;
243 protected AccountResource account;
247 List<Composite> treeViews = new ArrayList<Composite>();
249 @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
253 private HTML usedBytes;
255 private HTML totalBytes;
257 private HTML usedPercent;
259 private HTML numOfFiles;
261 private Toolbar toolbar;
263 private FileUploadDialog fileUploadDialog;
266 public void onModuleLoad() {
267 if (parseUserCredentials())
271 private void initialize() {
272 boolean bareContent = Window.Location.getParameter("noframe") != null;
273 String contentWidth = bareContent ? "100%" : "75%";
275 VerticalPanel outer = new VerticalPanel();
276 outer.setWidth("100%");
278 outer.addStyleName("pithos-outer");
282 topPanel = new TopPanel(this, Pithos.images);
283 topPanel.setWidth("100%");
285 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
288 messagePanel.setVisible(false);
289 outer.add(messagePanel);
290 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
291 outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
293 HorizontalPanel header = new HorizontalPanel();
294 header.addStyleName("pithos-header");
295 header.setWidth(contentWidth);
297 header.addStyleName("pithos-header-noframe");
298 upload = new Button("Upload", new ClickHandler() {
300 public void onClick(ClickEvent event) {
301 if (getSelection() != null)
302 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
305 upload.addStyleName("pithos-uploadButton");
307 header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
308 header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
310 toolbar = new Toolbar(this);
312 header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
313 header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
315 HorizontalPanel folderStatistics = new HorizontalPanel();
316 folderStatistics.addStyleName("pithos-folderStatistics");
317 numOfFiles = new HTML();
318 folderStatistics.add(numOfFiles);
319 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
320 HTML numOfFilesLabel = new HTML(" Files");
321 folderStatistics.add(numOfFilesLabel);
322 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
323 header.add(folderStatistics);
324 header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
325 header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
326 header.setCellWidth(folderStatistics, "40px");
328 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
329 // Inner contains the various lists.nner
330 inner.sinkEvents(Event.ONCONTEXTMENU);
331 inner.setWidth("100%");
333 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
334 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
336 public void onSelectionChange(SelectionChangeEvent event) {
337 if (folderTreeSelectionModel.getSelectedObject() != null) {
338 deselectOthers(folderTreeView, folderTreeSelectionModel);
339 applyPermissions(folderTreeSelectionModel.getSelectedObject());
340 Folder f = folderTreeSelectionModel.getSelectedObject();
341 updateFolder(f, true, new Command() {
344 public void execute() {
349 showRelevantToolbarButtons();
352 selectionModels.add(folderTreeSelectionModel);
354 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
355 folderTreeView = new FolderTreeView(folderTreeViewModel);
356 treeViews.add(folderTreeView);
358 fileList = new FileList(this, images, folderTreeView);
361 groupTreeViewModel = new GroupTreeViewModel(this);
362 groupTreeView = new GroupTreeView(groupTreeViewModel);
363 treeViews.add(groupTreeView);
365 trees = new VerticalPanel();
366 trees.setWidth("100%");
369 HorizontalPanel treeHeader = new HorizontalPanel();
370 treeHeader.addStyleName("pithos-treeHeader");
371 treeHeader.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
372 treeHeader.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
373 HorizontalPanel statistics = new HorizontalPanel();
374 statistics.addStyleName("pithos-statistics");
375 statistics.add(new HTML("Used: "));
376 usedBytes = new HTML();
377 statistics.add(usedBytes);
378 statistics.add(new HTML(" of "));
379 totalBytes = new HTML();
380 statistics.add(totalBytes);
381 statistics.add(new HTML(" ("));
382 usedPercent = new HTML();
383 statistics.add(usedPercent);
384 statistics.add(new HTML(")"));
385 treeHeader.add(statistics);
386 treeHeader.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_LEFT);
387 trees.add(treeHeader);
389 trees.add(folderTreeView);
390 trees.add(groupTreeView);
391 // Add the left and right panels to the split panel.
392 splitPanel.setLeftWidget(trees);
393 splitPanel.setRightWidget(inner);
394 splitPanel.setSplitPosition("35%");
395 splitPanel.setSize("100%", "100%");
396 splitPanel.addStyleName("pithos-splitPanel");
397 splitPanel.setWidth(contentWidth);
398 outer.add(splitPanel);
399 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
402 statusPanel = new StatusPanel();
403 statusPanel.setWidth("100%");
404 outer.add(statusPanel);
405 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
408 splitPanel.addStyleName("pithos-splitPanel-noframe");
410 // Hook the window resize event, so that we can adjust the UI.
411 Window.addResizeHandler(this);
412 // Clear out the window's built-in margin, because we want to take
413 // advantage of the entire client area.
414 Window.setMargin("0px");
415 // Finally, add the outer panel to the RootPanel, so that it will be
417 RootPanel.get().add(outer);
418 // Call the window resized handler to get the initial sizes setup. Doing
419 // this in a deferred command causes it to occur after all widgets'
420 // sizes have been computed by the browser.
421 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
424 public void execute() {
425 onWindowResized(Window.getClientHeight());
429 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
431 public void execute() {
432 fetchAccount(new Command() {
435 public void execute() {
436 if (!account.hasHomeContainer())
437 createHomeContainer(account, this);
438 else if (!account.hasTrashContainer())
439 createTrashContainer(this);
441 for (Folder f : account.getContainers())
442 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
446 folderTreeViewModel.initialize(account, new Command() {
449 public void execute() {
450 createMySharedTree();
453 groupTreeViewModel.initialize();
461 // Scheduler.get().scheduleDeferred(new Command() {
464 // public void execute() {
465 // displayError("lalala");
471 public void applyPermissions(Folder f) {
474 upload.setEnabled(false);
476 Boolean[] perms = f.getPermissions().get(username);
477 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
478 upload.setEnabled(true);
481 upload.setEnabled(false);
485 upload.setEnabled(false);
488 @SuppressWarnings({ "rawtypes", "unchecked" })
489 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
490 selectedTree = _selectedTree;
492 for (Composite c : treeViews)
493 if (c.equals(selectedTree))
494 c.addStyleName("cellTreeWidget-selectedTree");
496 c.removeStyleName("cellTreeWidget-selectedTree");
498 for (SingleSelectionModel s : selectionModels)
499 if (!s.equals(model))
500 s.setSelected(s.getSelectedObject(), false);
503 public void showFiles(final Folder f) {
504 Set<File> files = f.getFiles();
508 public void showFiles(Set<File> files) {
509 fileList.setFiles(new ArrayList<File>(files));
513 * Parse and store the user credentials to the appropriate fields.
515 private boolean parseUserCredentials() {
516 username = Window.Location.getParameter("user");
517 token = Window.Location.getParameter("token");
518 Configuration conf = (Configuration) GWT.create(Configuration.class);
519 if (username == null || username.length() == 0 || token == null || token.length() == 0) {
520 String cookie = conf.authCookie();
521 String auth = Cookies.getCookie(cookie);
526 if (auth.startsWith("\""))
527 auth = auth.substring(1);
528 if (auth.endsWith("\""))
529 auth = auth.substring(0, auth.length() - 1);
530 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
531 if (authSplit.length != 2) {
535 username = authSplit[0];
536 token = authSplit[1];
540 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token, null, "", "/", false);
545 * Redirect the user to the login page for authentication.
547 protected void authenticateUser() {
548 Window.Location.assign(config.loginUrl());
551 protected void fetchAccount(final Command callback) {
552 String path = "?format=json";
554 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
556 public void onSuccess(AccountResource _result) {
558 if (callback != null)
563 public void onError(Throwable t) {
564 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());
573 protected void onUnauthorized(Response response) {
577 getAccount.setHeader("X-Auth-Token", token);
578 Scheduler.get().scheduleDeferred(getAccount);
581 public void updateStatistics() {
582 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
585 public void onSuccess(AccountResource _result) {
590 public void onError(Throwable t) {
591 GWT.log("Error getting account", t);
593 if (t instanceof RestException)
594 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
596 displayError("System error fetching user data: " + t.getMessage());
600 protected void onUnauthorized(Response response) {
604 headAccount.setHeader("X-Auth-Token", token);
605 Scheduler.get().scheduleDeferred(headAccount);
608 protected void showStatistics() {
609 usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
610 totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
611 NumberFormat nf = NumberFormat.getPercentFormat();
612 usedPercent.setHTML(nf.format(account.getUsedPercentage()));
615 protected void createHomeContainer(final AccountResource _account, final Command callback) {
616 String path = "/" + Pithos.HOME_CONTAINER;
617 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
619 public void onSuccess(Resource result) {
620 if (!_account.hasTrashContainer())
621 createTrashContainer(callback);
623 fetchAccount(callback);
627 public void onError(Throwable t) {
628 GWT.log("Error creating pithos", t);
630 if (t instanceof RestException)
631 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
633 displayError("System error Error creating pithos: " + t.getMessage());
637 protected void onUnauthorized(Response response) {
641 createPithos.setHeader("X-Auth-Token", getToken());
642 Scheduler.get().scheduleDeferred(createPithos);
645 protected void createTrashContainer(final Command callback) {
646 String path = "/" + Pithos.TRASH_CONTAINER;
647 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
649 public void onSuccess(Resource result) {
650 fetchAccount(callback);
654 public void onError(Throwable t) {
655 GWT.log("Error creating pithos", t);
657 if (t instanceof RestException)
658 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
660 displayError("System error Error creating pithos: " + t.getMessage());
664 protected void onUnauthorized(Response response) {
668 createPithos.setHeader("X-Auth-Token", getToken());
669 Scheduler.get().scheduleDeferred(createPithos);
673 * Creates an HTML fragment that places an image & caption together, for use
676 * @param imageProto an image prototype for an image
677 * @param caption the group caption
678 * @return the header HTML fragment
680 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
681 String captionHTML = "<table class='caption' cellpadding='0' "
682 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
683 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
684 + caption + "</b></td></tr></table>";
688 protected void onWindowResized(int height) {
689 // Adjust the split panel to take up the available room in the window.
690 int newHeight = height - splitPanel.getAbsoluteTop();
693 splitPanel.setHeight("" + newHeight);
694 inner.setHeight("" + newHeight);
698 public void onResize(ResizeEvent event) {
699 int height = event.getHeight();
700 onWindowResized(height);
704 * Display an error message.
706 * @param msg the message to display
708 public void displayError(String msg) {
709 messagePanel.displayError(msg);
713 * Display a warning message.
715 * @param msg the message to display
717 public void displayWarning(String msg) {
718 messagePanel.displayWarning(msg);
722 * Display an informational message.
724 * @param msg the message to display
726 public void displayInformation(String msg) {
727 messagePanel.displayInformation(msg);
731 * Retrieve the fileList.
733 * @return the fileList
735 public FileList getFileList() {
740 * Retrieve the topPanel.
742 * @return the topPanel
744 TopPanel getTopPanel() {
749 * Retrieve the clipboard.
751 * @return the clipboard
753 public Clipboard getClipboard() {
757 public StatusPanel getStatusPanel() {
761 public String getToken() {
765 public static native void preventIESelection() /*-{
766 $doc.body.onselectstart = function () { return false; };
769 public static native void enableIESelection() /*-{
770 if ($doc.body.onselectstart != null)
771 $doc.body.onselectstart = null;
775 * @return the absolute path of the API root URL
777 public String getApiPath() {
778 Configuration conf = (Configuration) GWT.create(Configuration.class);
779 return conf.apiPath();
783 * History support for folder navigation
784 * adds a new browser history entry
788 public void updateHistory(String key){
789 // Replace any whitespace of the initial string to "+"
790 // String result = key.replaceAll("\\s","+");
791 // Add a new browser history entry.
792 // History.newItem(result);
793 History.newItem(key);
796 public void deleteFolder(final Folder folder) {
797 String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
798 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
799 builder.setHeader("X-Auth-Token", getToken());
801 builder.sendRequest("", new RequestCallback() {
803 public void onResponseReceived(Request request, Response response) {
804 if (response.getStatusCode() == Response.SC_OK) {
805 JSONValue json = JSONParser.parseStrict(response.getText());
806 JSONArray array = json.isArray();
809 deleteObject(folder, i, array);
815 public void onError(Request request, Throwable exception) {
817 displayError("System error unable to delete folder: " + exception.getMessage());
821 catch (RequestException e) {
825 void deleteObject(final Folder folder, final int i, final JSONArray array) {
826 if (i < array.size()) {
827 JSONObject o = array.get(i).isObject();
828 if (o != null && !o.containsKey("subdir")) {
829 JSONString name = o.get("name").isString();
830 String path = "/" + folder.getContainer() + "/" + name.stringValue();
831 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), URL.encode(path)) {
833 public void onSuccess(Resource result) {
834 deleteObject(folder, i + 1, array);
838 public void onError(Throwable t) {
841 displayError("System error unable to delete folder: " + t.getMessage());
845 protected void onUnauthorized(Response response) {
849 delete.setHeader("X-Auth-Token", getToken());
850 Scheduler.get().scheduleDeferred(delete);
852 else if (o != null) {
853 String subdir = o.get("subdir").isString().stringValue();
854 subdir = subdir.substring(0, subdir.length() - 1);
855 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
856 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
857 builder.setHeader("X-Auth-Token", getToken());
859 builder.sendRequest("", new RequestCallback() {
861 public void onResponseReceived(Request request, Response response) {
862 if (response.getStatusCode() == Response.SC_OK) {
863 JSONValue json = JSONParser.parseStrict(response.getText());
864 JSONArray array2 = json.isArray();
865 if (array2 != null) {
866 int l = array.size();
867 for (int j=0; j<array2.size(); j++) {
868 array.set(l++, array2.get(j));
871 deleteObject(folder, i + 1, array);
876 public void onError(Request request, Throwable exception) {
878 displayError("System error unable to delete folder: " + exception.getMessage());
882 catch (RequestException e) {
887 String path = folder.getUri();
888 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), URL.encode(path)) {
890 public void onSuccess(Resource result) {
891 updateFolder(folder.getParent(), true, new Command() {
894 public void execute() {
901 public void onError(Throwable t) {
904 if (t instanceof RestException) {
905 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
906 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
911 displayError("System error unable to delete folder: " + t.getMessage());
915 protected void onUnauthorized(Response response) {
919 deleteFolder.setHeader("X-Auth-Token", getToken());
920 Scheduler.get().scheduleDeferred(deleteFolder);
924 public FolderTreeView getFolderTreeView() {
925 return folderTreeView;
928 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
929 if (iter.hasNext()) {
930 File file = iter.next();
931 String path = targetUri + "/" + file.getName();
932 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
934 public void onSuccess(Resource result) {
935 copyFiles(iter, targetUsername, targetUri, callback);
939 public void onError(Throwable t) {
942 if (t instanceof RestException) {
943 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
946 displayError("System error unable to copy file: "+t.getMessage());
950 protected void onUnauthorized(Response response) {
954 copyFile.setHeader("X-Auth-Token", getToken());
955 copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
956 if (!file.getOwner().equals(targetUsername))
957 copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwner()));
958 copyFile.setHeader("Content-Type", file.getContentType());
959 Scheduler.get().scheduleDeferred(copyFile);
961 else if (callback != null) {
966 public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
967 if (iter.hasNext()) {
968 final Folder f = iter.next();
969 copyFolder(f, targetUsername, targetUri, new Command() {
972 public void execute() {
973 copySubfolders(iter, targetUsername, targetUri, callback);
977 else if (callback != null) {
982 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
983 String path = targetUri + "/" + f.getName();
984 PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
986 public void onSuccess(Resource result) {
987 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
990 public void onSuccess(final Folder _f) {
991 Iterator<File> iter = _f.getFiles().iterator();
992 copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
994 public void execute() {
995 Iterator<Folder> iterf = _f.getSubfolders().iterator();
996 copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1002 public void onError(Throwable t) {
1005 if (t instanceof RestException) {
1006 displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1009 displayError("System error getting folder: " + t.getMessage());
1013 protected void onUnauthorized(Response response) {
1017 getFolder.setHeader("X-Auth-Token", getToken());
1018 Scheduler.get().scheduleDeferred(getFolder);
1022 public void onError(Throwable t) {
1025 if (t instanceof RestException) {
1026 displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1029 displayError("System error creating folder: " + t.getMessage());
1033 protected void onUnauthorized(Response response) {
1037 createFolder.setHeader("X-Auth-Token", getToken());
1038 createFolder.setHeader("Accept", "*/*");
1039 createFolder.setHeader("Content-Length", "0");
1040 createFolder.setHeader("Content-Type", "application/folder");
1041 Scheduler.get().scheduleDeferred(createFolder);
1044 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1045 selectionModels.add(model);
1048 public OtherSharedTreeView getOtherSharedTreeView() {
1049 return otherSharedTreeView;
1052 public void updateTrash(boolean showFiles, Command callback) {
1053 updateFolder(trash, showFiles, callback);
1056 public void updateGroupsNode() {
1057 groupTreeView.updateGroupNode(null);
1060 public void addGroup(String groupname) {
1061 Group newGroup = new Group(groupname);
1062 account.addGroup(newGroup);
1063 groupTreeView.updateGroupNode(null);
1066 public void removeGroup(Group group) {
1067 account.removeGroup(group);
1071 public TreeView getSelectedTree() {
1072 return selectedTree;
1075 public Folder getSelection() {
1076 return selectedTree.getSelection();
1079 public void showFolderStatistics(int folderFileCount) {
1080 numOfFiles.setHTML(String.valueOf(folderFileCount));
1083 public GroupTreeView getGroupTreeView() {
1084 return groupTreeView;
1087 public void sessionExpired() {
1088 new SessionExpiredDialog(this).center();
1091 public void updateRootFolder(Command callback) {
1092 updateFolder(account.getPithos(), false, callback);
1095 void createMySharedTree() {
1096 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1097 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1099 public void onSelectionChange(SelectionChangeEvent event) {
1100 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1101 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1102 upload.setEnabled(false);
1103 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1105 showRelevantToolbarButtons();
1108 selectionModels.add(mysharedTreeSelectionModel);
1109 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1110 mysharedTreeViewModel.initialize(new Command() {
1113 public void execute() {
1114 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1115 trees.insert(mysharedTreeView, 2);
1116 treeViews.add(mysharedTreeView);
1117 createOtherSharedTree();
1122 void createOtherSharedTree() {
1123 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1124 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1126 public void onSelectionChange(SelectionChangeEvent event) {
1127 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1128 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1129 otherSharedTreeView.addStyleName("cellTreeWidget-selectedTree");
1130 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1131 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1133 showRelevantToolbarButtons();
1136 selectionModels.add(otherSharedTreeSelectionModel);
1137 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1138 otherSharedTreeViewModel.initialize(new Command() {
1141 public void execute() {
1142 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1143 trees.insert(otherSharedTreeView, 3);
1144 treeViews.add(otherSharedTreeView);
1149 public void logoff() {
1150 Cookies.removeCookie(config.authCookie(), "/");
1151 Cookies.removeCookie(config.authTokenCookie(), "/");
1152 for (String s: Cookies.getCookieNames())
1153 if (s.startsWith(config.shibSessionCookiePrefix()))
1154 Cookies.removeCookie(s, "/");
1155 Window.Location.assign(config.logoutUrl());
1158 public native void log1(String message)/*-{
1159 $wnd.console.log(message);
1162 public String getErrorData() {
1164 return error.toString();
1168 public void setError(Throwable t) {
1172 public void showRelevantToolbarButtons() {
1173 toolbar.showRelevantButtons();
1176 public FileUploadDialog getFileUploadDialog() {
1177 if (fileUploadDialog == null)
1178 fileUploadDialog = new FileUploadDialog(this);
1179 return fileUploadDialog;
1182 public void hideUploadIndicator() {
1183 upload.removeStyleName("pithos-uploadButton-loading");
1184 upload.setTitle("");
1187 public void showUploadIndicator() {
1188 upload.addStyleName("pithos-uploadButton-loading");
1189 upload.setTitle("Upload in progress. Click for details.");