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.Date;
62 import java.util.HashMap;
63 import java.util.Iterator;
64 import java.util.List;
67 import com.google.gwt.core.client.EntryPoint;
68 import com.google.gwt.core.client.GWT;
69 import com.google.gwt.core.client.Scheduler;
70 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
71 import com.google.gwt.event.dom.client.ClickEvent;
72 import com.google.gwt.event.dom.client.ClickHandler;
73 import com.google.gwt.event.logical.shared.ResizeEvent;
74 import com.google.gwt.event.logical.shared.ResizeHandler;
75 import com.google.gwt.http.client.Request;
76 import com.google.gwt.http.client.RequestBuilder;
77 import com.google.gwt.http.client.RequestCallback;
78 import com.google.gwt.http.client.RequestException;
79 import com.google.gwt.http.client.Response;
80 import com.google.gwt.http.client.URL;
81 import com.google.gwt.i18n.client.NumberFormat;
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.Composite;
96 import com.google.gwt.user.client.ui.HTML;
97 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
98 import com.google.gwt.user.client.ui.HasVerticalAlignment;
99 import com.google.gwt.user.client.ui.HorizontalPanel;
100 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
101 import com.google.gwt.user.client.ui.RootPanel;
102 import com.google.gwt.user.client.ui.VerticalPanel;
103 import com.google.gwt.view.client.SelectionChangeEvent;
104 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
105 import com.google.gwt.view.client.SingleSelectionModel;
108 * Entry point classes define <code>onModuleLoad()</code>.
110 public class Pithos implements EntryPoint, ResizeHandler {
112 public static final String HOME_CONTAINER = "pithos";
114 public static final String TRASH_CONTAINER = "trash";
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 updateSharedFolder(Folder f, boolean showfiles) {
143 mysharedTreeView.updateFolder(f, showfiles);
146 public void updateOtherSharedFolder(Folder f, boolean showfiles) {
147 otherSharedTreeView.updateFolder(f, showfiles);
150 public List<Tag> getAllTags() {
151 List<Tag> tagList = new ArrayList<Tag>();
152 for (Folder f : account.getContainers()) {
153 for (String t : f.getTags()) {
154 tagList.add(new Tag(t));
160 public MysharedTreeView getMySharedTreeView() {
161 return mysharedTreeView;
165 * An aggregate image bundle that pulls together all the images for this
166 * application into a single bundle.
168 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
170 @Source("gr/grnet/pithos/resources/document.png")
171 ImageResource folders();
173 @Source("gr/grnet/pithos/resources/advancedsettings.png")
174 ImageResource tools();
178 * The Application Clipboard implementation;
180 private Clipboard clipboard = new Clipboard();
183 * The top panel that contains the menu bar.
185 private TopPanel topPanel;
188 * The panel that contains the various system messages.
190 private MessagePanel messagePanel = new MessagePanel(Pithos.images);
193 * The bottom panel that contains the status bar.
195 private StatusPanel statusPanel = null;
198 * The file list widget.
200 private FileList fileList;
203 * The tab panel that occupies the right side of the screen.
205 private VerticalPanel inner = new VerticalPanel();
209 * The split panel that will contain the left and right panels.
211 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
214 * The currently selected item in the application, for use by the Edit menu
215 * commands. Potential types are Folder, File, User and Group.
217 private Object currentSelection;
219 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
221 private String username = null;
224 * The authentication token of the current user.
226 private String token;
230 SingleSelectionModel<Folder> folderTreeSelectionModel;
231 FolderTreeViewModel folderTreeViewModel;
232 FolderTreeView folderTreeView;
234 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
235 MysharedTreeViewModel mysharedTreeViewModel;
236 MysharedTreeView mysharedTreeView = null;;
238 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
239 OtherSharedTreeViewModel otherSharedTreeViewModel;
240 OtherSharedTreeView otherSharedTreeView = null;
242 GroupTreeViewModel groupTreeViewModel;
243 private GroupTreeView groupTreeView;
245 private TreeView selectedTree;
246 protected AccountResource account;
250 List<Composite> treeViews = new ArrayList<Composite>();
252 @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
256 private HTML totalFiles;
258 private HTML usedBytes;
260 private HTML totalBytes;
262 private HTML usedPercent;
264 private HTML numOfFiles;
266 private Button toolsButton;
269 public void onModuleLoad() {
270 if (parseUserCredentials())
274 private void initialize() {
275 boolean bareContent = Window.Location.getParameter("noframe") != null;
276 String contentWidth = bareContent ? "100%" : "75%";
278 VerticalPanel outer = new VerticalPanel();
279 outer.setWidth("100%");
281 outer.addStyleName("pithos-outer");
285 topPanel = new TopPanel(this, Pithos.images);
286 topPanel.setWidth("100%");
288 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
291 HorizontalPanel header = new HorizontalPanel();
292 header.addStyleName("pithos-header");
293 header.setWidth(contentWidth);
295 header.addStyleName("pithos-header-noframe");
296 upload = new Button("Upload File", new ClickHandler() {
298 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
299 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
302 upload.addStyleName("pithos-uploadButton");
304 header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
305 header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
306 header.setCellWidth(upload, "146px");
308 messagePanel.setVisible(false);
309 header.add(messagePanel);
310 header.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
311 header.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
314 toolsButton = new Button(AbstractImagePrototype.create(images.tools()).getHTML());
315 toolsButton.addClickHandler(new ClickHandler() {
318 public void onClick(ClickEvent event) {
319 ToolsMenu menu = new ToolsMenu(Pithos.this, images, getSelectedTree(), getSelectedTree().getSelection(), getFileList().getSelectedFiles());
320 if (!menu.isEmpty()) {
321 menu.setPopupPosition(event.getClientX(), event.getClientY());
326 header.add(toolsButton);
327 header.setCellHorizontalAlignment(toolsButton, HasHorizontalAlignment.ALIGN_RIGHT);
328 header.setCellVerticalAlignment(toolsButton, HasVerticalAlignment.ALIGN_MIDDLE);
329 header.setCellWidth(toolsButton, "30px");
331 HorizontalPanel folderStatistics = new HorizontalPanel();
332 folderStatistics.addStyleName("pithos-folderStatistics");
333 numOfFiles = new HTML();
334 folderStatistics.add(numOfFiles);
335 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
336 HTML numOfFilesLabel = new HTML(" Files");
337 folderStatistics.add(numOfFilesLabel);
338 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
339 header.add(folderStatistics);
340 header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
341 header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
342 header.setCellWidth(folderStatistics, "60px");
344 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
345 // Inner contains the various lists.nner
346 inner.sinkEvents(Event.ONCONTEXTMENU);
347 inner.setWidth("100%");
349 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
350 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
352 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
353 if (folderTreeSelectionModel.getSelectedObject() != null) {
354 deselectOthers(folderTreeView, folderTreeSelectionModel);
355 applyPermissions(folderTreeSelectionModel.getSelectedObject());
356 Folder f = folderTreeSelectionModel.getSelectedObject();
361 selectionModels.add(folderTreeSelectionModel);
363 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
364 folderTreeView = new FolderTreeView(folderTreeViewModel);
365 treeViews.add(folderTreeView);
367 fileList = new FileList(this, images, folderTreeView);
370 groupTreeViewModel = new GroupTreeViewModel(this);
371 groupTreeView = new GroupTreeView(groupTreeViewModel);
372 treeViews.add(groupTreeView);
374 trees = new VerticalPanel();
375 trees.setWidth("100%");
378 HorizontalPanel treeHeader = new HorizontalPanel();
379 treeHeader.addStyleName("pithos-treeHeader");
380 treeHeader.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
381 treeHeader.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
382 HorizontalPanel statistics = new HorizontalPanel();
383 statistics.addStyleName("pithos-statistics");
384 statistics.add(new HTML("Total Objects: "));
385 totalFiles = new HTML();
386 statistics.add(totalFiles);
387 statistics.add(new HTML(" | Used: "));
388 usedBytes = new HTML();
389 statistics.add(usedBytes);
390 statistics.add(new HTML(" of "));
391 totalBytes = new HTML();
392 statistics.add(totalBytes);
393 statistics.add(new HTML(" ("));
394 usedPercent = new HTML();
395 statistics.add(usedPercent);
396 statistics.add(new HTML(")"));
397 treeHeader.add(statistics);
398 treeHeader.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_LEFT);
399 trees.add(treeHeader);
401 trees.add(folderTreeView);
402 trees.add(groupTreeView);
403 // Add the left and right panels to the split panel.
404 splitPanel.setLeftWidget(trees);
405 splitPanel.setRightWidget(inner);
406 splitPanel.setSplitPosition("35%");
407 splitPanel.setSize("100%", "100%");
408 splitPanel.addStyleName("pithos-splitPanel");
409 splitPanel.setWidth(contentWidth);
410 outer.add(splitPanel);
411 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
414 statusPanel = new StatusPanel();
415 statusPanel.setWidth("100%");
416 outer.add(statusPanel);
417 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
420 splitPanel.addStyleName("pithos-splitPanel-noframe");
422 // Hook the window resize event, so that we can adjust the UI.
423 Window.addResizeHandler(this);
424 // Clear out the window's built-in margin, because we want to take
425 // advantage of the entire client area.
426 Window.setMargin("0px");
427 // Finally, add the outer panel to the RootPanel, so that it will be
429 RootPanel.get().add(outer);
430 // Call the window resized handler to get the initial sizes setup. Doing
431 // this in a deferred command causes it to occur after all widgets'
432 // sizes have been computed by the browser.
433 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
436 public void execute() {
437 onWindowResized(Window.getClientHeight());
441 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
443 public void execute() {
444 fetchAccount(new Command() {
447 public void execute() {
448 if (!account.hasHomeContainer())
449 createHomeContainer(account, this);
450 else if (!account.hasTrashContainer())
451 createTrashContainer(this);
453 for (Folder f : account.getContainers())
454 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
458 folderTreeViewModel.initialize(account, new Command() {
461 public void execute() {
462 createMySharedTree();
465 groupTreeViewModel.initialize();
473 // Scheduler.get().scheduleDeferred(new Command() {
476 // public void execute() {
477 // displayError("lalala");
483 public void applyPermissions(Folder f) {
486 upload.setEnabled(false);
488 Boolean[] perms = f.getPermissions().get(username);
489 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
490 upload.setEnabled(true);
493 upload.setEnabled(false);
497 upload.setEnabled(false);
500 @SuppressWarnings({ "rawtypes", "unchecked" })
501 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
502 selectedTree = _selectedTree;
504 for (Composite c : treeViews)
505 if (c.equals(selectedTree))
506 c.addStyleName("cellTreeWidget-selectedTree");
508 c.removeStyleName("cellTreeWidget-selectedTree");
510 for (SingleSelectionModel s : selectionModels)
511 if (!s.equals(model))
512 s.setSelected(s.getSelectedObject(), false);
515 public void showFiles(Folder f) {
516 Set<File> files = f.getFiles();
520 public void showFiles(Set<File> files) {
521 //Iterator<File> iter = files.iterator();
522 //fetchFile(iter, files);
523 fileList.setFiles(new ArrayList<File>(files));
526 protected void fetchFile(final Iterator<File> iter, final Set<File> files) {
527 if (iter.hasNext()) {
528 File file = iter.next();
529 String path = file.getUri() + "?format=json";
530 GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
532 public void onSuccess(@SuppressWarnings("unused") File _result) {
533 fetchFile(iter, files);
537 public void onError(Throwable t) {
538 GWT.log("Error getting file", t);
539 if (t instanceof RestException)
540 displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
542 displayError("System error fetching file: " + t.getMessage());
546 protected void onUnauthorized(Response response) {
550 getFile.setHeader("X-Auth-Token", "0000");
551 Scheduler.get().scheduleDeferred(getFile);
554 fileList.setFiles(new ArrayList<File>(files));
558 * Parse and store the user credentials to the appropriate fields.
560 private boolean parseUserCredentials() {
561 username = Window.Location.getParameter("user");
562 token = Window.Location.getParameter("token");
563 Configuration conf = (Configuration) GWT.create(Configuration.class);
564 if (username == null || username.length() == 0 || token == null || token.length() == 0) {
565 String cookie = conf.authCookie();
566 String auth = Cookies.getCookie(cookie);
571 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
572 if (authSplit.length != 2) {
576 username = authSplit[0];
577 token = authSplit[1];
581 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token, null, "", "/", false);
586 * Redirect the user to the login page for authentication.
588 protected void authenticateUser() {
589 Configuration conf = (Configuration) GWT.create(Configuration.class);
590 Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
593 protected void fetchAccount(final Command callback) {
594 String path = "?format=json";
596 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
598 public void onSuccess(AccountResource _result) {
600 if (callback != null)
605 public void onError(Throwable t) {
606 GWT.log("Error getting account", t);
607 if (t instanceof RestException)
608 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
610 displayError("System error fetching user data: " + t.getMessage());
614 protected void onUnauthorized(Response response) {
618 getAccount.setHeader("X-Auth-Token", token);
619 Scheduler.get().scheduleDeferred(getAccount);
622 public void updateStatistics() {
623 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
626 public void onSuccess(@SuppressWarnings("unused") AccountResource _result) {
631 public void onError(Throwable t) {
632 GWT.log("Error getting account", t);
633 if (t instanceof RestException)
634 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
636 displayError("System error fetching user data: " + t.getMessage());
640 protected void onUnauthorized(Response response) {
644 headAccount.setHeader("X-Auth-Token", token);
645 Scheduler.get().scheduleDeferred(headAccount);
648 protected void showStatistics() {
649 totalFiles.setHTML(String.valueOf(account.getNumberOfObjects()));
650 usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
651 totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
652 NumberFormat nf = NumberFormat.getPercentFormat();
653 usedPercent.setHTML(nf.format(account.getUsedPercentage()));
656 protected void createHomeContainer(final AccountResource _account, final Command callback) {
657 String path = "/" + Pithos.HOME_CONTAINER;
658 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
660 public void onSuccess(@SuppressWarnings("unused") Resource result) {
661 if (!_account.hasTrashContainer())
662 createTrashContainer(callback);
664 fetchAccount(callback);
668 public void onError(Throwable t) {
669 GWT.log("Error creating pithos", t);
670 if (t instanceof RestException)
671 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
673 displayError("System error Error creating pithos: " + t.getMessage());
677 protected void onUnauthorized(Response response) {
681 createPithos.setHeader("X-Auth-Token", getToken());
682 Scheduler.get().scheduleDeferred(createPithos);
685 protected void createTrashContainer(final Command callback) {
686 String path = "/" + Pithos.TRASH_CONTAINER;
687 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
689 public void onSuccess(@SuppressWarnings("unused") Resource result) {
690 fetchAccount(callback);
694 public void onError(Throwable t) {
695 GWT.log("Error creating pithos", t);
696 if (t instanceof RestException)
697 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
699 displayError("System error Error creating pithos: " + t.getMessage());
703 protected void onUnauthorized(Response response) {
707 createPithos.setHeader("X-Auth-Token", getToken());
708 Scheduler.get().scheduleDeferred(createPithos);
712 * Creates an HTML fragment that places an image & caption together, for use
715 * @param imageProto an image prototype for an image
716 * @param caption the group caption
717 * @return the header HTML fragment
719 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
720 String captionHTML = "<table class='caption' cellpadding='0' "
721 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
722 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
723 + caption + "</b></td></tr></table>";
727 protected void onWindowResized(int height) {
728 // Adjust the split panel to take up the available room in the window.
729 int newHeight = height - splitPanel.getAbsoluteTop();
732 splitPanel.setHeight("" + newHeight);
733 inner.setHeight("" + newHeight);
737 public void onResize(ResizeEvent event) {
738 int height = event.getHeight();
739 onWindowResized(height);
743 * Display an error message.
745 * @param msg the message to display
747 public void displayError(String msg) {
748 messagePanel.displayError(msg);
752 * Display a warning message.
754 * @param msg the message to display
756 public void displayWarning(String msg) {
757 messagePanel.displayWarning(msg);
761 * Display an informational message.
763 * @param msg the message to display
765 public void displayInformation(String msg) {
766 messagePanel.displayInformation(msg);
770 * Retrieve the fileList.
772 * @return the fileList
774 public FileList getFileList() {
779 * Retrieve the topPanel.
781 * @return the topPanel
783 TopPanel getTopPanel() {
788 * Retrieve the clipboard.
790 * @return the clipboard
792 public Clipboard getClipboard() {
796 public StatusPanel getStatusPanel() {
800 public String getToken() {
804 public static native void preventIESelection() /*-{
805 $doc.body.onselectstart = function () { return false; };
808 public static native void enableIESelection() /*-{
809 if ($doc.body.onselectstart != null)
810 $doc.body.onselectstart = null;
814 * @return the absolute path of the API root URL
816 public String getApiPath() {
817 Configuration conf = (Configuration) GWT.create(Configuration.class);
818 return conf.apiPath();
822 * History support for folder navigation
823 * adds a new browser history entry
827 public void updateHistory(String key){
828 // Replace any whitespace of the initial string to "+"
829 // String result = key.replaceAll("\\s","+");
830 // Add a new browser history entry.
831 // History.newItem(result);
832 History.newItem(key);
835 public void deleteFolder(final Folder folder) {
836 String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
837 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
838 builder.setHeader("X-Auth-Token", getToken());
840 builder.sendRequest("", new RequestCallback() {
842 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
843 if (response.getStatusCode() == Response.SC_OK) {
844 JSONValue json = JSONParser.parseStrict(response.getText());
845 JSONArray array = json.isArray();
848 deleteObject(folder, i, array);
854 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
855 displayError("System error unable to delete folder: " + exception.getMessage());
859 catch (RequestException e) {
863 void deleteObject(final Folder folder, final int i, final JSONArray array) {
864 if (i < array.size()) {
865 JSONObject o = array.get(i).isObject();
866 if (o != null && !o.containsKey("subdir")) {
867 JSONString name = o.get("name").isString();
868 String path = "/" + folder.getContainer() + "/" + name.stringValue();
869 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), path) {
871 public void onSuccess(@SuppressWarnings("unused") Resource result) {
872 deleteObject(folder, i + 1, array);
876 public void onError(Throwable t) {
878 displayError("System error unable to delete folder: " + t.getMessage());
882 protected void onUnauthorized(Response response) {
886 delete.setHeader("X-Auth-Token", getToken());
887 Scheduler.get().scheduleDeferred(delete);
889 else if (o != null) {
890 String subdir = o.get("subdir").isString().stringValue();
891 subdir = subdir.substring(0, subdir.length() - 1);
892 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
893 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
894 builder.setHeader("X-Auth-Token", getToken());
896 builder.sendRequest("", new RequestCallback() {
898 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
899 if (response.getStatusCode() == Response.SC_OK) {
900 JSONValue json = JSONParser.parseStrict(response.getText());
901 JSONArray array2 = json.isArray();
902 if (array2 != null) {
903 int l = array.size();
904 for (int j=0; j<array2.size(); j++) {
905 array.set(l++, array2.get(j));
908 deleteObject(folder, i + 1, array);
913 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
914 displayError("System error unable to delete folder: " + exception.getMessage());
918 catch (RequestException e) {
923 String path = folder.getUri();
924 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
926 public void onSuccess(@SuppressWarnings("unused") Resource result) {
927 updateFolder(folder.getParent(), true, new Command() {
930 public void execute() {
937 public void onError(Throwable t) {
939 if (t instanceof RestException) {
940 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
941 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
946 displayError("System error unable to delete folder: " + t.getMessage());
950 protected void onUnauthorized(Response response) {
954 deleteFolder.setHeader("X-Auth-Token", getToken());
955 Scheduler.get().scheduleDeferred(deleteFolder);
959 public FolderTreeView getFolderTreeView() {
960 return folderTreeView;
963 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
964 if (iter.hasNext()) {
965 File file = iter.next();
966 String path = targetUri + "/" + file.getName();
967 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
969 public void onSuccess(@SuppressWarnings("unused") Resource result) {
970 copyFiles(iter, targetUsername, targetUri, callback);
974 public void onError(Throwable t) {
976 if (t instanceof RestException) {
977 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
980 displayError("System error unable to copy file: "+t.getMessage());
984 protected void onUnauthorized(Response response) {
988 copyFile.setHeader("X-Auth-Token", getToken());
989 copyFile.setHeader("X-Copy-From", file.getUri());
990 if (!file.getOwner().equals(targetUsername))
991 copyFile.setHeader("X-Source-Account", file.getOwner());
992 Scheduler.get().scheduleDeferred(copyFile);
994 else if (callback != null) {
999 public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
1000 if (iter.hasNext()) {
1001 final Folder f = iter.next();
1002 copyFolder(f, targetUsername, targetUri, new Command() {
1005 public void execute() {
1006 copySubfolders(iter, targetUsername, targetUri, callback);
1010 else if (callback != null) {
1015 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
1016 String path = targetUri + "/" + f.getName();
1017 PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
1019 public void onSuccess(@SuppressWarnings("unused") Resource result) {
1020 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
1023 public void onSuccess(final Folder _f) {
1024 Iterator<File> iter = _f.getFiles().iterator();
1025 copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
1027 public void execute() {
1028 Iterator<Folder> iterf = _f.getSubfolders().iterator();
1029 copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1035 public void onError(Throwable t) {
1037 if (t instanceof RestException) {
1038 displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1041 displayError("System error getting folder: " + t.getMessage());
1045 protected void onUnauthorized(Response response) {
1049 getFolder.setHeader("X-Auth-Token", getToken());
1050 Scheduler.get().scheduleDeferred(getFolder);
1054 public void onError(Throwable t) {
1056 if (t instanceof RestException) {
1057 displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1060 displayError("System error creating folder: " + t.getMessage());
1064 protected void onUnauthorized(Response response) {
1068 createFolder.setHeader("X-Auth-Token", getToken());
1069 createFolder.setHeader("Accept", "*/*");
1070 createFolder.setHeader("Content-Length", "0");
1071 createFolder.setHeader("Content-Type", "application/folder");
1072 Scheduler.get().scheduleDeferred(createFolder);
1075 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1076 selectionModels.add(model);
1079 public OtherSharedTreeView getOtherSharedTreeView() {
1080 return otherSharedTreeView;
1083 public void updateTrash(boolean showFiles, Command callback) {
1084 updateFolder(trash, showFiles, callback);
1087 public void updateGroupsNode() {
1088 groupTreeView.updateGroupNode(null);
1091 public void addGroup(String groupname) {
1092 Group newGroup = new Group(groupname);
1093 account.addGroup(newGroup);
1094 groupTreeView.updateGroupNode(null);
1097 public void removeGroup(Group group) {
1098 account.removeGroup(group);
1102 public TreeView getSelectedTree() {
1103 return selectedTree;
1106 public Folder getSelection() {
1107 return selectedTree.getSelection();
1110 public void showFolderStatistics(int folderFileCount) {
1111 numOfFiles.setHTML(String.valueOf(folderFileCount));
1114 public GroupTreeView getGroupTreeView() {
1115 return groupTreeView;
1118 public void sessionExpired() {
1119 new SessionExpiredDialog(this).center();
1122 public void updateRootFolder(Command callback) {
1123 updateFolder(account.getPithos(), false, callback);
1126 void createMySharedTree() {
1127 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1128 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1130 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
1131 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1132 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1133 upload.setEnabled(false);
1134 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1138 selectionModels.add(mysharedTreeSelectionModel);
1139 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1140 mysharedTreeViewModel.initialize(new Command() {
1143 public void execute() {
1144 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1145 trees.insert(mysharedTreeView, 2);
1146 treeViews.add(mysharedTreeView);
1147 createOtherSharedTree();
1152 void createOtherSharedTree() {
1153 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1154 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1156 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
1157 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1158 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1159 otherSharedTreeView.addStyleName("cellTreeWidget-selectedTree");
1160 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1161 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1165 selectionModels.add(otherSharedTreeSelectionModel);
1166 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1167 otherSharedTreeViewModel.initialize(new Command() {
1170 public void execute() {
1171 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1172 trees.insert(otherSharedTreeView, 3);
1173 treeViews.add(otherSharedTreeView);
1178 public void logoff() {
1179 Configuration conf = (Configuration) GWT.create(Configuration.class);
1180 Cookies.removeCookie(conf.authCookie());
1181 Cookies.removeCookie(conf.authTokenCookie(), "/");
1182 for (String s: Cookies.getCookieNames())
1183 if (s.startsWith(conf.shibSessionCookiePrefix()))
1184 Cookies.removeCookie(s, "/");
1185 Window.Location.assign(Window.Location.getPath());