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.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;
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.http.client.URL;
80 import com.google.gwt.i18n.client.DateTimeFormat;
81 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
82 import com.google.gwt.json.client.JSONArray;
83 import com.google.gwt.json.client.JSONObject;
84 import com.google.gwt.json.client.JSONParser;
85 import com.google.gwt.json.client.JSONString;
86 import com.google.gwt.json.client.JSONValue;
87 import com.google.gwt.resources.client.ImageResource;
88 import com.google.gwt.user.client.Command;
89 import com.google.gwt.user.client.Cookies;
90 import com.google.gwt.user.client.Event;
91 import com.google.gwt.user.client.History;
92 import com.google.gwt.user.client.Window;
93 import com.google.gwt.user.client.ui.AbstractImagePrototype;
94 import com.google.gwt.user.client.ui.Button;
95 import com.google.gwt.user.client.ui.HTML;
96 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
97 import com.google.gwt.user.client.ui.HasVerticalAlignment;
98 import com.google.gwt.user.client.ui.HorizontalPanel;
99 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
100 import com.google.gwt.user.client.ui.RootPanel;
101 import com.google.gwt.user.client.ui.VerticalPanel;
102 import com.google.gwt.view.client.SelectionChangeEvent;
103 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
104 import com.google.gwt.view.client.SingleSelectionModel;
107 * Entry point classes define <code>onModuleLoad()</code>.
109 public class Pithos implements EntryPoint, ResizeHandler {
111 public static final String HOME_CONTAINER = "pithos";
113 public static final String TRASH_CONTAINER = "trash";
116 * Instantiate an application-level image bundle. This object will provide
117 * programmatic access to all the images needed by widgets.
119 static Images images = (Images) GWT.create(Images.class);
121 public String getUsername() {
125 public void setAccount(AccountResource acct) {
129 public AccountResource getAccount() {
133 public void updateFolder(Folder f, boolean showfiles, Command callback) {
134 folderTreeView.updateFolder(f, showfiles, callback);
137 public void updateGroupNode(Group group) {
138 groupTreeView.updateGroupNode(group);
141 public void updateSharedFolder(Folder f, boolean showfiles) {
142 mysharedTreeView.updateFolder(f, showfiles);
145 public void updateOtherSharedFolder(Folder f, boolean showfiles) {
146 otherSharedTreeView.updateFolder(f, showfiles);
149 public void updateTag(Tag t) {
150 tagTreeView.updateTag(t);
153 public void updateTags() {
154 tagTreeViewModel.initialize(getAllTags());
157 public List<Tag> getAllTags() {
158 List<Tag> tagList = new ArrayList<Tag>();
159 for (Folder f : account.getContainers()) {
160 for (String t : f.getTags()) {
161 tagList.add(new Tag(t));
167 public MysharedTreeView getMySharedTreeView() {
168 return mysharedTreeView;
172 * An aggregate image bundle that pulls together all the images for this
173 * application into a single bundle.
175 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
177 @Source("gr/grnet/pithos/resources/document.png")
178 ImageResource folders();
180 @Source("gr/grnet/pithos/resources/advancedsettings.png")
181 ImageResource tools();
185 * The Application Clipboard implementation;
187 private Clipboard clipboard = new Clipboard();
190 * The top panel that contains the menu bar.
192 private TopPanel topPanel;
195 * The panel that contains the various system messages.
197 private MessagePanel messagePanel = new MessagePanel(Pithos.images);
200 * The bottom panel that contains the status bar.
202 private StatusPanel statusPanel = null;
205 * The file list widget.
207 private FileList fileList;
210 * The tab panel that occupies the right side of the screen.
212 private VerticalPanel inner = new VerticalPanel();
216 * The split panel that will contain the left and right panels.
218 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
221 * The currently selected item in the application, for use by the Edit menu
222 * commands. Potential types are Folder, File, User and Group.
224 private Object currentSelection;
226 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
228 private String username = null;
231 * The authentication token of the current user.
233 private String token;
235 SingleSelectionModel<Folder> folderTreeSelectionModel;
236 FolderTreeViewModel folderTreeViewModel;
237 FolderTreeView folderTreeView;
239 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
240 private MysharedTreeViewModel mysharedTreeViewModel;
241 MysharedTreeView mysharedTreeView;
243 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
244 private OtherSharedTreeViewModel otherSharedTreeViewModel;
245 OtherSharedTreeView otherSharedTreeView;
247 protected SingleSelectionModel<Tag> tagTreeSelectionModel;
248 private TagTreeViewModel tagTreeViewModel;
249 private TagTreeView tagTreeView;
251 GroupTreeViewModel groupTreeViewModel;
252 private GroupTreeView groupTreeView;
254 private TreeView selectedTree;
255 protected AccountResource account;
259 @SuppressWarnings("rawtypes")
260 private List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
264 private HTML totalFiles;
266 private HTML usedBytes;
268 private HTML totalBytes;
270 private HTML usedPercent;
272 private HTML numOfFiles;
274 private Button toolsButton;
277 public void onModuleLoad() {
278 if (parseUserCredentials())
282 private void initialize() {
283 VerticalPanel outer = new VerticalPanel();
284 outer.setWidth("100%");
286 topPanel = new TopPanel(this, Pithos.images);
287 topPanel.setWidth("100%");
290 messagePanel.setWidth("100%");
291 messagePanel.setVisible(false);
292 outer.add(messagePanel);
293 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
296 // Inner contains the various lists.
297 inner.sinkEvents(Event.ONCONTEXTMENU);
298 inner.setWidth("100%");
300 HorizontalPanel rightside = new HorizontalPanel();
301 rightside.addStyleName("pithos-rightSide");
302 rightside.setSpacing(5);
304 toolsButton = new Button(AbstractImagePrototype.create(images.tools()).getHTML());
305 toolsButton.addClickHandler(new ClickHandler() {
308 public void onClick(ClickEvent event) {
309 ToolsMenu menu = new ToolsMenu(Pithos.this, images, getSelectedTree(), getSelectedTree().getSelection(), getFileList().getSelectedFiles());
310 if (!menu.isEmpty()) {
311 menu.setPopupPosition(event.getClientX(), event.getClientY());
316 rightside.add(toolsButton);
317 rightside.setCellHorizontalAlignment(toolsButton, HasHorizontalAlignment.ALIGN_LEFT);
319 HorizontalPanel folderStatistics = new HorizontalPanel();
320 folderStatistics.addStyleName("pithos-folderStatistics");
321 numOfFiles = new HTML();
322 folderStatistics.add(numOfFiles);
323 HTML numOfFilesLabel = new HTML(" Files");
324 folderStatistics.add(numOfFilesLabel);
325 rightside.add(folderStatistics);
326 rightside.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
328 inner.add(rightside);
329 inner.setCellVerticalAlignment(rightside, HasVerticalAlignment.ALIGN_MIDDLE);
330 inner.setCellHeight(rightside, "60px");
332 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
333 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
335 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
336 if (folderTreeSelectionModel.getSelectedObject() != null) {
337 deselectOthers(folderTreeView, folderTreeSelectionModel);
338 applyPermissions(folderTreeSelectionModel.getSelectedObject());
339 Folder f = folderTreeSelectionModel.getSelectedObject();
340 updateFolder(f, true, null);
344 selectionModels.add(folderTreeSelectionModel);
346 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
347 folderTreeView = new FolderTreeView(folderTreeViewModel);
349 fileList = new FileList(this, images, folderTreeView);
352 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
353 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
355 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
356 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
357 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
358 upload.setEnabled(false);
359 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
363 selectionModels.add(mysharedTreeSelectionModel);
364 mysharedTreeViewModel = new MysharedTreeViewModel(this, mysharedTreeSelectionModel);
365 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
367 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
368 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
370 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
371 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
372 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
373 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
374 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
378 selectionModels.add(otherSharedTreeSelectionModel);
379 otherSharedTreeViewModel = new OtherSharedTreeViewModel(this, otherSharedTreeSelectionModel);
380 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
382 groupTreeViewModel = new GroupTreeViewModel(this);
383 groupTreeView = new GroupTreeView(groupTreeViewModel);
385 VerticalPanel trees = new VerticalPanel();
387 upload = new Button("Upload File", new ClickHandler() {
389 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
390 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
393 upload.addStyleName("pithos-uploadButton");
396 HorizontalPanel treeHeader = new HorizontalPanel();
397 treeHeader.addStyleName("pithos-treeHeader");
398 HorizontalPanel statistics = new HorizontalPanel();
399 statistics.add(new HTML("Total Objects: "));
400 totalFiles = new HTML();
401 statistics.add(totalFiles);
402 statistics.add(new HTML(" | Used: "));
403 usedBytes = new HTML();
404 statistics.add(usedBytes);
405 statistics.add(new HTML(" of "));
406 totalBytes = new HTML();
407 statistics.add(totalBytes);
408 statistics.add(new HTML(" ("));
409 usedPercent = new HTML();
410 statistics.add(usedPercent);
411 statistics.add(new HTML("%)"));
412 treeHeader.add(statistics);
413 trees.add(treeHeader);
415 trees.add(folderTreeView);
416 trees.add(mysharedTreeView);
417 trees.add(otherSharedTreeView);
418 // trees.add(tagTreeView);
419 trees.add(groupTreeView);
420 // Add the left and right panels to the split panel.
421 splitPanel.setLeftWidget(trees);
422 splitPanel.setRightWidget(inner);
423 splitPanel.setSplitPosition("25%");
424 splitPanel.setSize("100%", "100%");
425 splitPanel.addStyleName("pithos-splitPanel");
426 outer.add(splitPanel);
428 statusPanel = new StatusPanel();
429 outer.add(statusPanel);
432 // Hook the window resize event, so that we can adjust the UI.
433 Window.addResizeHandler(this);
434 // Clear out the window's built-in margin, because we want to take
435 // advantage of the entire client area.
436 Window.setMargin("0px");
437 // Finally, add the outer panel to the RootPanel, so that it will be
439 RootPanel.get().add(outer);
440 // Call the window resized handler to get the initial sizes setup. Doing
441 // this in a deferred command causes it to occur after all widgets'
442 // sizes have been computed by the browser.
443 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
446 public void execute() {
447 onWindowResized(Window.getClientHeight());
451 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
453 public void execute() {
454 fetchAccount(new Command() {
457 public void execute() {
458 if (!account.hasHomeContainer())
459 createHomeContainer(account, this);
460 else if (!account.hasTrashContainer())
461 createTrashContainer(this);
463 for (Folder f : account.getContainers())
464 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
468 folderTreeViewModel.initialize(account);
469 groupTreeViewModel.initialize();
478 public void applyPermissions(Folder f) {
481 upload.setEnabled(false);
483 Boolean[] perms = f.getPermissions().get(username);
484 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
485 upload.setEnabled(true);
488 upload.setEnabled(false);
492 upload.setEnabled(false);
495 @SuppressWarnings({ "rawtypes", "unchecked" })
496 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
497 selectedTree = _selectedTree;
498 for (SingleSelectionModel s : selectionModels)
499 if (!s.equals(model))
500 s.setSelected(s.getSelectedObject(), false);
503 public void showFiles(Folder f) {
504 Set<File> files = f.getFiles();
508 public void showFiles(Set<File> files) {
509 //Iterator<File> iter = files.iterator();
510 //fetchFile(iter, files);
511 fileList.setFiles(new ArrayList<File>(files));
514 protected void fetchFile(final Iterator<File> iter, final Set<File> files) {
515 if (iter.hasNext()) {
516 File file = iter.next();
517 String path = file.getUri() + "?format=json";
518 GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
520 public void onSuccess(@SuppressWarnings("unused") File _result) {
521 fetchFile(iter, files);
525 public void onError(Throwable t) {
526 GWT.log("Error getting file", t);
527 if (t instanceof RestException)
528 displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
530 displayError("System error fetching file: " + t.getMessage());
534 protected void onUnauthorized(Response response) {
538 getFile.setHeader("X-Auth-Token", "0000");
539 Scheduler.get().scheduleDeferred(getFile);
542 fileList.setFiles(new ArrayList<File>(files));
546 * Parse and store the user credentials to the appropriate fields.
548 private boolean parseUserCredentials() {
549 username = Window.Location.getParameter("user");
550 token = Window.Location.getParameter("token");
551 Configuration conf = (Configuration) GWT.create(Configuration.class);
552 if (username == null || username.length() == 0 || token == null || token.length() == 0) {
553 String cookie = conf.authCookie();
554 String auth = Cookies.getCookie(cookie);
559 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
560 if (authSplit.length != 2) {
564 username = authSplit[0];
565 token = authSplit[1];
568 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token);
573 * Redirect the user to the login page for authentication.
575 protected void authenticateUser() {
576 Configuration conf = (Configuration) GWT.create(Configuration.class);
577 Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
580 protected void fetchAccount(final Command callback) {
581 String path = "?format=json";
583 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
585 public void onSuccess(AccountResource _result) {
587 if (callback != null)
592 public void onError(Throwable t) {
593 GWT.log("Error getting account", t);
594 if (t instanceof RestException)
595 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
597 displayError("System error fetching user data: " + t.getMessage());
601 protected void onUnauthorized(Response response) {
605 getAccount.setHeader("X-Auth-Token", token);
606 Scheduler.get().scheduleDeferred(getAccount);
609 public void updateStatistics() {
610 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
613 public void onSuccess(@SuppressWarnings("unused") AccountResource _result) {
618 public void onError(Throwable t) {
619 GWT.log("Error getting account", t);
620 if (t instanceof RestException)
621 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
623 displayError("System error fetching user data: " + t.getMessage());
627 protected void onUnauthorized(Response response) {
631 headAccount.setHeader("X-Auth-Token", token);
632 Scheduler.get().scheduleDeferred(headAccount);
635 protected void showStatistics() {
636 totalFiles.setHTML(String.valueOf(account.getNumberOfObjects()));
637 usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
638 totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
639 usedPercent.setHTML(String.valueOf(account.getUsedPercentage()));
642 protected void createHomeContainer(final AccountResource _account, final Command callback) {
643 String path = "/" + Pithos.HOME_CONTAINER;
644 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
646 public void onSuccess(@SuppressWarnings("unused") Resource result) {
647 if (!_account.hasTrashContainer())
648 createTrashContainer(callback);
650 fetchAccount(callback);
654 public void onError(Throwable t) {
655 GWT.log("Error creating pithos", t);
656 if (t instanceof RestException)
657 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
659 displayError("System error Error creating pithos: " + t.getMessage());
663 protected void onUnauthorized(Response response) {
667 createPithos.setHeader("X-Auth-Token", getToken());
668 Scheduler.get().scheduleDeferred(createPithos);
671 protected void createTrashContainer(final Command callback) {
672 String path = "/" + Pithos.TRASH_CONTAINER;
673 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
675 public void onSuccess(@SuppressWarnings("unused") Resource result) {
676 fetchAccount(callback);
680 public void onError(Throwable t) {
681 GWT.log("Error creating pithos", t);
682 if (t instanceof RestException)
683 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
685 displayError("System error Error creating pithos: " + t.getMessage());
689 protected void onUnauthorized(Response response) {
693 createPithos.setHeader("X-Auth-Token", getToken());
694 Scheduler.get().scheduleDeferred(createPithos);
698 * Creates an HTML fragment that places an image & caption together, for use
701 * @param imageProto an image prototype for an image
702 * @param caption the group caption
703 * @return the header HTML fragment
705 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
706 String captionHTML = "<table class='caption' cellpadding='0' "
707 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
708 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
709 + caption + "</b></td></tr></table>";
713 protected void onWindowResized(int height) {
714 // Adjust the split panel to take up the available room in the window.
715 int newHeight = height - splitPanel.getAbsoluteTop() - 60;
718 splitPanel.setHeight("" + newHeight);
719 inner.setHeight("" + newHeight);
723 public void onResize(ResizeEvent event) {
724 int height = event.getHeight();
725 onWindowResized(height);
729 * Display an error message.
731 * @param msg the message to display
733 public void displayError(String msg) {
734 messagePanel.displayError(msg);
738 * Display a warning message.
740 * @param msg the message to display
742 public void displayWarning(String msg) {
743 messagePanel.displayWarning(msg);
747 * Display an informational message.
749 * @param msg the message to display
751 public void displayInformation(String msg) {
752 messagePanel.displayInformation(msg);
756 * Retrieve the fileList.
758 * @return the fileList
760 public FileList getFileList() {
765 * Retrieve the topPanel.
767 * @return the topPanel
769 TopPanel getTopPanel() {
774 * Retrieve the clipboard.
776 * @return the clipboard
778 public Clipboard getClipboard() {
782 public StatusPanel getStatusPanel() {
786 public String getToken() {
790 public static native void preventIESelection() /*-{
791 $doc.body.onselectstart = function () { return false; };
794 public static native void enableIESelection() /*-{
795 if ($doc.body.onselectstart != null)
796 $doc.body.onselectstart = null;
800 * @return the absolute path of the API root URL
802 public String getApiPath() {
803 Configuration conf = (Configuration) GWT.create(Configuration.class);
804 return conf.apiPath();
808 * History support for folder navigation
809 * adds a new browser history entry
813 public void updateHistory(String key){
814 // Replace any whitespace of the initial string to "+"
815 // String result = key.replaceAll("\\s","+");
816 // Add a new browser history entry.
817 // History.newItem(result);
818 History.newItem(key);
821 public void deleteFolder(final Folder folder) {
822 String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
823 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
824 builder.setHeader("X-Auth-Token", getToken());
826 builder.sendRequest("", new RequestCallback() {
828 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
829 if (response.getStatusCode() == Response.SC_OK) {
830 JSONValue json = JSONParser.parseStrict(response.getText());
831 JSONArray array = json.isArray();
834 deleteObject(folder, i, array);
840 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
841 displayError("System error unable to delete folder: " + exception.getMessage());
845 catch (RequestException e) {
849 void deleteObject(final Folder folder, final int i, final JSONArray array) {
850 if (i < array.size()) {
851 JSONObject o = array.get(i).isObject();
852 if (o != null && !o.containsKey("subdir")) {
853 JSONString name = o.get("name").isString();
854 String path = "/" + folder.getContainer() + "/" + name.stringValue();
855 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), path) {
857 public void onSuccess(@SuppressWarnings("unused") Resource result) {
858 deleteObject(folder, i + 1, array);
862 public void onError(Throwable t) {
864 displayError("System error unable to delete folder: " + t.getMessage());
868 protected void onUnauthorized(Response response) {
872 delete.setHeader("X-Auth-Token", getToken());
873 Scheduler.get().scheduleDeferred(delete);
875 else if (o != null) {
876 String subdir = o.get("subdir").isString().stringValue();
877 subdir = subdir.substring(0, subdir.length() - 1);
878 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
879 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
880 builder.setHeader("X-Auth-Token", getToken());
882 builder.sendRequest("", new RequestCallback() {
884 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
885 if (response.getStatusCode() == Response.SC_OK) {
886 JSONValue json = JSONParser.parseStrict(response.getText());
887 JSONArray array2 = json.isArray();
888 if (array2 != null) {
889 int l = array.size();
890 for (int j=0; j<array2.size(); j++) {
891 array.set(l++, array2.get(j));
894 deleteObject(folder, i + 1, array);
899 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
900 displayError("System error unable to delete folder: " + exception.getMessage());
904 catch (RequestException e) {
909 String path = folder.getUri();
910 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
912 public void onSuccess(@SuppressWarnings("unused") Resource result) {
913 updateFolder(folder.getParent(), true, new Command() {
916 public void execute() {
923 public void onError(Throwable t) {
925 if (t instanceof RestException) {
926 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
927 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
932 displayError("System error unable to delete folder: " + t.getMessage());
936 protected void onUnauthorized(Response response) {
940 deleteFolder.setHeader("X-Auth-Token", getToken());
941 Scheduler.get().scheduleDeferred(deleteFolder);
945 public FolderTreeView getFolderTreeView() {
946 return folderTreeView;
949 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
950 if (iter.hasNext()) {
951 File file = iter.next();
952 String path = targetUri + "/" + file.getName();
953 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
955 public void onSuccess(@SuppressWarnings("unused") Resource result) {
956 copyFiles(iter, targetUsername, targetUri, callback);
960 public void onError(Throwable t) {
962 if (t instanceof RestException) {
963 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
966 displayError("System error unable to copy file: "+t.getMessage());
970 protected void onUnauthorized(Response response) {
974 copyFile.setHeader("X-Auth-Token", getToken());
975 copyFile.setHeader("X-Copy-From", file.getUri());
976 Scheduler.get().scheduleDeferred(copyFile);
978 else if (callback != null) {
983 public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
984 if (iter.hasNext()) {
985 final Folder f = iter.next();
986 copyFolder(f, targetUsername, targetUri, new Command() {
989 public void execute() {
990 copySubfolders(iter, targetUsername, targetUri, callback);
994 else if (callback != null) {
999 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
1000 String path = targetUri + "/" + f.getName();
1001 PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
1003 public void onSuccess(@SuppressWarnings("unused") Resource result) {
1004 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
1007 public void onSuccess(final Folder _f) {
1008 Iterator<File> iter = _f.getFiles().iterator();
1009 copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
1011 public void execute() {
1012 Iterator<Folder> iterf = _f.getSubfolders().iterator();
1013 copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1019 public void onError(Throwable t) {
1021 if (t instanceof RestException) {
1022 displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1025 displayError("System error getting folder: " + t.getMessage());
1029 protected void onUnauthorized(Response response) {
1033 getFolder.setHeader("X-Auth-Token", getToken());
1034 Scheduler.get().scheduleDeferred(getFolder);
1038 public void onError(Throwable t) {
1040 if (t instanceof RestException) {
1041 displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1044 displayError("System error creating folder: " + t.getMessage());
1048 protected void onUnauthorized(Response response) {
1052 createFolder.setHeader("X-Auth-Token", getToken());
1053 createFolder.setHeader("Accept", "*/*");
1054 createFolder.setHeader("Content-Length", "0");
1055 createFolder.setHeader("Content-Type", "application/folder");
1056 Scheduler.get().scheduleDeferred(createFolder);
1059 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1060 selectionModels.add(model);
1063 public OtherSharedTreeView getOtherSharedTreeView() {
1064 return otherSharedTreeView;
1067 public void updateTrash(boolean showFiles, Command callback) {
1068 updateFolder(trash, showFiles, callback);
1071 public void updateGroupsNode() {
1072 groupTreeView.updateGroupNode(null);
1075 public void addGroup(String groupname) {
1076 Group newGroup = new Group(groupname);
1077 account.addGroup(newGroup);
1078 groupTreeView.updateGroupNode(null);
1081 public void removeGroup(Group group) {
1082 account.removeGroup(group);
1086 public TreeView getSelectedTree() {
1087 return selectedTree;
1090 public Folder getSelection() {
1091 return selectedTree.getSelection();
1094 public void showFolderStatistics(int folderFileCount) {
1095 numOfFiles.setHTML(String.valueOf(folderFileCount));
1098 public GroupTreeView getGroupTreeView() {
1099 return groupTreeView;
1102 public void sessionExpired() {
1103 new SessionExpiredDialog(this).center();