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.resources.client.ImageResource.ImageOptions;
89 import com.google.gwt.user.client.Command;
90 import com.google.gwt.user.client.Cookies;
91 import com.google.gwt.user.client.Event;
92 import com.google.gwt.user.client.History;
93 import com.google.gwt.user.client.Window;
94 import com.google.gwt.user.client.ui.AbstractImagePrototype;
95 import com.google.gwt.user.client.ui.Button;
96 import com.google.gwt.user.client.ui.Composite;
97 import com.google.gwt.user.client.ui.HTML;
98 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
99 import com.google.gwt.user.client.ui.HasVerticalAlignment;
100 import com.google.gwt.user.client.ui.HorizontalPanel;
101 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
102 import com.google.gwt.user.client.ui.Image;
103 import com.google.gwt.user.client.ui.RootPanel;
104 import com.google.gwt.user.client.ui.VerticalPanel;
105 import com.google.gwt.view.client.SelectionChangeEvent;
106 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
107 import com.google.gwt.view.client.SingleSelectionModel;
110 * Entry point classes define <code>onModuleLoad()</code>.
112 public class Pithos implements EntryPoint, ResizeHandler {
114 public static final String HOME_CONTAINER = "pithos";
116 public static final String TRASH_CONTAINER = "trash";
119 * Instantiate an application-level image bundle. This object will provide
120 * programmatic access to all the images needed by widgets.
122 static Images images = (Images) GWT.create(Images.class);
124 public String getUsername() {
128 public void setAccount(AccountResource acct) {
132 public AccountResource getAccount() {
136 public void updateFolder(Folder f, boolean showfiles, Command callback) {
137 folderTreeView.updateFolder(f, showfiles, callback);
140 public void updateGroupNode(Group group) {
141 groupTreeView.updateGroupNode(group);
144 public void updateSharedFolder(Folder f, boolean showfiles) {
145 mysharedTreeView.updateFolder(f, showfiles);
148 public void updateOtherSharedFolder(Folder f, boolean showfiles) {
149 otherSharedTreeView.updateFolder(f, showfiles);
152 public List<Tag> getAllTags() {
153 List<Tag> tagList = new ArrayList<Tag>();
154 for (Folder f : account.getContainers()) {
155 for (String t : f.getTags()) {
156 tagList.add(new Tag(t));
162 public MysharedTreeView getMySharedTreeView() {
163 return mysharedTreeView;
167 * An aggregate image bundle that pulls together all the images for this
168 * application into a single bundle.
170 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
172 @Source("gr/grnet/pithos/resources/document.png")
173 ImageResource folders();
175 @Source("gr/grnet/pithos/resources/advancedsettings.png")
176 @ImageOptions(width=32, height=32)
177 ImageResource tools();
181 * The Application Clipboard implementation;
183 private Clipboard clipboard = new Clipboard();
186 * The top panel that contains the menu bar.
188 private TopPanel topPanel;
191 * The panel that contains the various system messages.
193 private MessagePanel messagePanel = new MessagePanel(Pithos.images);
196 * The bottom panel that contains the status bar.
198 private StatusPanel statusPanel = null;
201 * The file list widget.
203 private FileList fileList;
206 * The tab panel that occupies the right side of the screen.
208 private VerticalPanel inner = new VerticalPanel();
212 * The split panel that will contain the left and right panels.
214 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
217 * The currently selected item in the application, for use by the Edit menu
218 * commands. Potential types are Folder, File, User and Group.
220 private Object currentSelection;
222 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
224 private String username = null;
227 * The authentication token of the current user.
229 private String token;
233 SingleSelectionModel<Folder> folderTreeSelectionModel;
234 FolderTreeViewModel folderTreeViewModel;
235 FolderTreeView folderTreeView;
237 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
238 MysharedTreeViewModel mysharedTreeViewModel;
239 MysharedTreeView mysharedTreeView = null;;
241 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
242 OtherSharedTreeViewModel otherSharedTreeViewModel;
243 OtherSharedTreeView otherSharedTreeView = null;
245 GroupTreeViewModel groupTreeViewModel;
246 private GroupTreeView groupTreeView;
248 private TreeView selectedTree;
249 protected AccountResource account;
253 List<Composite> treeViews = new ArrayList<Composite>();
255 @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
259 private HTML usedBytes;
261 private HTML totalBytes;
263 private HTML usedPercent;
265 private HTML numOfFiles;
267 private Image toolsButton;
270 public void onModuleLoad() {
271 if (parseUserCredentials())
275 private void initialize() {
276 boolean bareContent = Window.Location.getParameter("noframe") != null;
277 String contentWidth = bareContent ? "100%" : "75%";
279 VerticalPanel outer = new VerticalPanel();
280 outer.setWidth("100%");
282 outer.addStyleName("pithos-outer");
286 topPanel = new TopPanel(this, Pithos.images);
287 topPanel.setWidth("100%");
289 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
292 HorizontalPanel header = new HorizontalPanel();
293 header.addStyleName("pithos-header");
294 header.setWidth(contentWidth);
296 header.addStyleName("pithos-header-noframe");
297 upload = new Button("Upload File", new ClickHandler() {
299 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
300 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
303 upload.addStyleName("pithos-uploadButton");
305 header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
306 header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
307 // header.setCellWidth(upload, "146px");
309 messagePanel.setVisible(false);
310 header.add(messagePanel);
311 header.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
312 header.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
314 toolsButton = new Image(images.tools());
315 toolsButton.addStyleName("pithos-toolsButton");
316 toolsButton.addClickHandler(new ClickHandler() {
319 public void onClick(ClickEvent event) {
320 ToolsMenu menu = new ToolsMenu(Pithos.this, images, getSelectedTree(), getSelectedTree().getSelection(), getFileList().getSelectedFiles());
321 if (!menu.isEmpty()) {
322 menu.setPopupPosition(event.getClientX(), event.getClientY());
327 header.add(toolsButton);
328 header.setCellHorizontalAlignment(toolsButton, HasHorizontalAlignment.ALIGN_CENTER);
329 header.setCellVerticalAlignment(toolsButton, HasVerticalAlignment.ALIGN_MIDDLE);
330 header.setCellWidth(toolsButton, "40px");
332 HorizontalPanel folderStatistics = new HorizontalPanel();
333 folderStatistics.addStyleName("pithos-folderStatistics");
334 numOfFiles = new HTML();
335 folderStatistics.add(numOfFiles);
336 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
337 HTML numOfFilesLabel = new HTML(" Files");
338 folderStatistics.add(numOfFilesLabel);
339 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
340 header.add(folderStatistics);
341 header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
342 header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
343 header.setCellWidth(folderStatistics, "40px");
345 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
346 // Inner contains the various lists.nner
347 inner.sinkEvents(Event.ONCONTEXTMENU);
348 inner.setWidth("100%");
350 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
351 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
353 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
354 if (folderTreeSelectionModel.getSelectedObject() != null) {
355 deselectOthers(folderTreeView, folderTreeSelectionModel);
356 applyPermissions(folderTreeSelectionModel.getSelectedObject());
357 Folder f = folderTreeSelectionModel.getSelectedObject();
362 selectionModels.add(folderTreeSelectionModel);
364 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
365 folderTreeView = new FolderTreeView(folderTreeViewModel);
366 treeViews.add(folderTreeView);
368 fileList = new FileList(this, images, folderTreeView);
371 groupTreeViewModel = new GroupTreeViewModel(this);
372 groupTreeView = new GroupTreeView(groupTreeViewModel);
373 treeViews.add(groupTreeView);
375 trees = new VerticalPanel();
376 trees.setWidth("100%");
379 HorizontalPanel treeHeader = new HorizontalPanel();
380 treeHeader.addStyleName("pithos-treeHeader");
381 treeHeader.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
382 treeHeader.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
383 HorizontalPanel statistics = new HorizontalPanel();
384 statistics.addStyleName("pithos-statistics");
385 statistics.add(new HTML("Used: "));
386 usedBytes = new HTML();
387 statistics.add(usedBytes);
388 statistics.add(new HTML(" of "));
389 totalBytes = new HTML();
390 statistics.add(totalBytes);
391 statistics.add(new HTML(" ("));
392 usedPercent = new HTML();
393 statistics.add(usedPercent);
394 statistics.add(new HTML(")"));
395 treeHeader.add(statistics);
396 treeHeader.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_LEFT);
397 trees.add(treeHeader);
399 trees.add(folderTreeView);
400 trees.add(groupTreeView);
401 // Add the left and right panels to the split panel.
402 splitPanel.setLeftWidget(trees);
403 splitPanel.setRightWidget(inner);
404 splitPanel.setSplitPosition("35%");
405 splitPanel.setSize("100%", "100%");
406 splitPanel.addStyleName("pithos-splitPanel");
407 splitPanel.setWidth(contentWidth);
408 outer.add(splitPanel);
409 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
412 statusPanel = new StatusPanel();
413 statusPanel.setWidth("100%");
414 outer.add(statusPanel);
415 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
418 splitPanel.addStyleName("pithos-splitPanel-noframe");
420 // Hook the window resize event, so that we can adjust the UI.
421 Window.addResizeHandler(this);
422 // Clear out the window's built-in margin, because we want to take
423 // advantage of the entire client area.
424 Window.setMargin("0px");
425 // Finally, add the outer panel to the RootPanel, so that it will be
427 RootPanel.get().add(outer);
428 // Call the window resized handler to get the initial sizes setup. Doing
429 // this in a deferred command causes it to occur after all widgets'
430 // sizes have been computed by the browser.
431 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
434 public void execute() {
435 onWindowResized(Window.getClientHeight());
439 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
441 public void execute() {
442 fetchAccount(new Command() {
445 public void execute() {
446 if (!account.hasHomeContainer())
447 createHomeContainer(account, this);
448 else if (!account.hasTrashContainer())
449 createTrashContainer(this);
451 for (Folder f : account.getContainers())
452 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
456 folderTreeViewModel.initialize(account, new Command() {
459 public void execute() {
460 createMySharedTree();
463 groupTreeViewModel.initialize();
471 // Scheduler.get().scheduleDeferred(new Command() {
474 // public void execute() {
475 // displayError("lalala");
481 public void applyPermissions(Folder f) {
484 upload.setEnabled(false);
486 Boolean[] perms = f.getPermissions().get(username);
487 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
488 upload.setEnabled(true);
491 upload.setEnabled(false);
495 upload.setEnabled(false);
498 @SuppressWarnings({ "rawtypes", "unchecked" })
499 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
500 selectedTree = _selectedTree;
502 for (Composite c : treeViews)
503 if (c.equals(selectedTree))
504 c.addStyleName("cellTreeWidget-selectedTree");
506 c.removeStyleName("cellTreeWidget-selectedTree");
508 for (SingleSelectionModel s : selectionModels)
509 if (!s.equals(model))
510 s.setSelected(s.getSelectedObject(), false);
513 public void showFiles(Folder f) {
514 Set<File> files = f.getFiles();
518 public void showFiles(Set<File> files) {
519 //Iterator<File> iter = files.iterator();
520 //fetchFile(iter, files);
521 fileList.setFiles(new ArrayList<File>(files));
524 protected void fetchFile(final Iterator<File> iter, final Set<File> files) {
525 if (iter.hasNext()) {
526 File file = iter.next();
527 String path = file.getUri() + "?format=json";
528 GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
530 public void onSuccess(@SuppressWarnings("unused") File _result) {
531 fetchFile(iter, files);
535 public void onError(Throwable t) {
536 GWT.log("Error getting file", t);
537 if (t instanceof RestException)
538 displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
540 displayError("System error fetching file: " + t.getMessage());
544 protected void onUnauthorized(Response response) {
548 getFile.setHeader("X-Auth-Token", "0000");
549 Scheduler.get().scheduleDeferred(getFile);
552 fileList.setFiles(new ArrayList<File>(files));
556 * Parse and store the user credentials to the appropriate fields.
558 private boolean parseUserCredentials() {
559 username = Window.Location.getParameter("user");
560 token = Window.Location.getParameter("token");
561 Configuration conf = (Configuration) GWT.create(Configuration.class);
562 if (username == null || username.length() == 0 || token == null || token.length() == 0) {
563 String cookie = conf.authCookie();
564 String auth = Cookies.getCookie(cookie);
569 if (auth.startsWith("\""))
570 auth = auth.substring(1);
571 if (auth.endsWith("\""))
572 auth = auth.substring(0, auth.length() - 1);
573 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
574 if (authSplit.length != 2) {
578 username = authSplit[0];
579 token = authSplit[1];
583 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token, null, "", "/", false);
588 * Redirect the user to the login page for authentication.
590 protected void authenticateUser() {
591 Configuration conf = (Configuration) GWT.create(Configuration.class);
592 Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
595 protected void fetchAccount(final Command callback) {
596 String path = "?format=json";
598 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
600 public void onSuccess(AccountResource _result) {
602 if (callback != null)
607 public void onError(Throwable t) {
608 GWT.log("Error getting account", t);
609 if (t instanceof RestException)
610 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
612 displayError("System error fetching user data: " + t.getMessage());
616 protected void onUnauthorized(Response response) {
620 getAccount.setHeader("X-Auth-Token", token);
621 Scheduler.get().scheduleDeferred(getAccount);
624 public void updateStatistics() {
625 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
628 public void onSuccess(@SuppressWarnings("unused") AccountResource _result) {
633 public void onError(Throwable t) {
634 GWT.log("Error getting account", t);
635 if (t instanceof RestException)
636 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
638 displayError("System error fetching user data: " + t.getMessage());
642 protected void onUnauthorized(Response response) {
646 headAccount.setHeader("X-Auth-Token", token);
647 Scheduler.get().scheduleDeferred(headAccount);
650 protected void showStatistics() {
651 usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
652 totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
653 NumberFormat nf = NumberFormat.getPercentFormat();
654 usedPercent.setHTML(nf.format(account.getUsedPercentage()));
657 protected void createHomeContainer(final AccountResource _account, final Command callback) {
658 String path = "/" + Pithos.HOME_CONTAINER;
659 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
661 public void onSuccess(@SuppressWarnings("unused") Resource result) {
662 if (!_account.hasTrashContainer())
663 createTrashContainer(callback);
665 fetchAccount(callback);
669 public void onError(Throwable t) {
670 GWT.log("Error creating pithos", t);
671 if (t instanceof RestException)
672 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
674 displayError("System error Error creating pithos: " + t.getMessage());
678 protected void onUnauthorized(Response response) {
682 createPithos.setHeader("X-Auth-Token", getToken());
683 Scheduler.get().scheduleDeferred(createPithos);
686 protected void createTrashContainer(final Command callback) {
687 String path = "/" + Pithos.TRASH_CONTAINER;
688 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
690 public void onSuccess(@SuppressWarnings("unused") Resource result) {
691 fetchAccount(callback);
695 public void onError(Throwable t) {
696 GWT.log("Error creating pithos", t);
697 if (t instanceof RestException)
698 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
700 displayError("System error Error creating pithos: " + t.getMessage());
704 protected void onUnauthorized(Response response) {
708 createPithos.setHeader("X-Auth-Token", getToken());
709 Scheduler.get().scheduleDeferred(createPithos);
713 * Creates an HTML fragment that places an image & caption together, for use
716 * @param imageProto an image prototype for an image
717 * @param caption the group caption
718 * @return the header HTML fragment
720 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
721 String captionHTML = "<table class='caption' cellpadding='0' "
722 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
723 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
724 + caption + "</b></td></tr></table>";
728 protected void onWindowResized(int height) {
729 // Adjust the split panel to take up the available room in the window.
730 int newHeight = height - splitPanel.getAbsoluteTop();
733 splitPanel.setHeight("" + newHeight);
734 inner.setHeight("" + newHeight);
738 public void onResize(ResizeEvent event) {
739 int height = event.getHeight();
740 onWindowResized(height);
744 * Display an error message.
746 * @param msg the message to display
748 public void displayError(String msg) {
749 messagePanel.displayError(msg);
753 * Display a warning message.
755 * @param msg the message to display
757 public void displayWarning(String msg) {
758 messagePanel.displayWarning(msg);
762 * Display an informational message.
764 * @param msg the message to display
766 public void displayInformation(String msg) {
767 messagePanel.displayInformation(msg);
771 * Retrieve the fileList.
773 * @return the fileList
775 public FileList getFileList() {
780 * Retrieve the topPanel.
782 * @return the topPanel
784 TopPanel getTopPanel() {
789 * Retrieve the clipboard.
791 * @return the clipboard
793 public Clipboard getClipboard() {
797 public StatusPanel getStatusPanel() {
801 public String getToken() {
805 public static native void preventIESelection() /*-{
806 $doc.body.onselectstart = function () { return false; };
809 public static native void enableIESelection() /*-{
810 if ($doc.body.onselectstart != null)
811 $doc.body.onselectstart = null;
815 * @return the absolute path of the API root URL
817 public String getApiPath() {
818 Configuration conf = (Configuration) GWT.create(Configuration.class);
819 return conf.apiPath();
823 * History support for folder navigation
824 * adds a new browser history entry
828 public void updateHistory(String key){
829 // Replace any whitespace of the initial string to "+"
830 // String result = key.replaceAll("\\s","+");
831 // Add a new browser history entry.
832 // History.newItem(result);
833 History.newItem(key);
836 public void deleteFolder(final Folder folder) {
837 String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
838 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
839 builder.setHeader("X-Auth-Token", getToken());
841 builder.sendRequest("", new RequestCallback() {
843 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
844 if (response.getStatusCode() == Response.SC_OK) {
845 JSONValue json = JSONParser.parseStrict(response.getText());
846 JSONArray array = json.isArray();
849 deleteObject(folder, i, array);
855 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
856 displayError("System error unable to delete folder: " + exception.getMessage());
860 catch (RequestException e) {
864 void deleteObject(final Folder folder, final int i, final JSONArray array) {
865 if (i < array.size()) {
866 JSONObject o = array.get(i).isObject();
867 if (o != null && !o.containsKey("subdir")) {
868 JSONString name = o.get("name").isString();
869 String path = "/" + folder.getContainer() + "/" + name.stringValue();
870 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), path) {
872 public void onSuccess(@SuppressWarnings("unused") Resource result) {
873 deleteObject(folder, i + 1, array);
877 public void onError(Throwable t) {
879 displayError("System error unable to delete folder: " + t.getMessage());
883 protected void onUnauthorized(Response response) {
887 delete.setHeader("X-Auth-Token", getToken());
888 Scheduler.get().scheduleDeferred(delete);
890 else if (o != null) {
891 String subdir = o.get("subdir").isString().stringValue();
892 subdir = subdir.substring(0, subdir.length() - 1);
893 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
894 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
895 builder.setHeader("X-Auth-Token", getToken());
897 builder.sendRequest("", new RequestCallback() {
899 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
900 if (response.getStatusCode() == Response.SC_OK) {
901 JSONValue json = JSONParser.parseStrict(response.getText());
902 JSONArray array2 = json.isArray();
903 if (array2 != null) {
904 int l = array.size();
905 for (int j=0; j<array2.size(); j++) {
906 array.set(l++, array2.get(j));
909 deleteObject(folder, i + 1, array);
914 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
915 displayError("System error unable to delete folder: " + exception.getMessage());
919 catch (RequestException e) {
924 String path = folder.getUri();
925 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
927 public void onSuccess(@SuppressWarnings("unused") Resource result) {
928 updateFolder(folder.getParent(), true, new Command() {
931 public void execute() {
938 public void onError(Throwable t) {
940 if (t instanceof RestException) {
941 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
942 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
947 displayError("System error unable to delete folder: " + t.getMessage());
951 protected void onUnauthorized(Response response) {
955 deleteFolder.setHeader("X-Auth-Token", getToken());
956 Scheduler.get().scheduleDeferred(deleteFolder);
960 public FolderTreeView getFolderTreeView() {
961 return folderTreeView;
964 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
965 if (iter.hasNext()) {
966 File file = iter.next();
967 String path = targetUri + "/" + file.getName();
968 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
970 public void onSuccess(@SuppressWarnings("unused") Resource result) {
971 copyFiles(iter, targetUsername, targetUri, callback);
975 public void onError(Throwable t) {
977 if (t instanceof RestException) {
978 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
981 displayError("System error unable to copy file: "+t.getMessage());
985 protected void onUnauthorized(Response response) {
989 copyFile.setHeader("X-Auth-Token", getToken());
990 copyFile.setHeader("X-Copy-From", file.getUri());
991 if (!file.getOwner().equals(targetUsername))
992 copyFile.setHeader("X-Source-Account", file.getOwner());
993 Scheduler.get().scheduleDeferred(copyFile);
995 else if (callback != null) {
1000 public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
1001 if (iter.hasNext()) {
1002 final Folder f = iter.next();
1003 copyFolder(f, targetUsername, targetUri, new Command() {
1006 public void execute() {
1007 copySubfolders(iter, targetUsername, targetUri, callback);
1011 else if (callback != null) {
1016 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
1017 String path = targetUri + "/" + f.getName();
1018 PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
1020 public void onSuccess(@SuppressWarnings("unused") Resource result) {
1021 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
1024 public void onSuccess(final Folder _f) {
1025 Iterator<File> iter = _f.getFiles().iterator();
1026 copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
1028 public void execute() {
1029 Iterator<Folder> iterf = _f.getSubfolders().iterator();
1030 copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1036 public void onError(Throwable t) {
1038 if (t instanceof RestException) {
1039 displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1042 displayError("System error getting folder: " + t.getMessage());
1046 protected void onUnauthorized(Response response) {
1050 getFolder.setHeader("X-Auth-Token", getToken());
1051 Scheduler.get().scheduleDeferred(getFolder);
1055 public void onError(Throwable t) {
1057 if (t instanceof RestException) {
1058 displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1061 displayError("System error creating folder: " + t.getMessage());
1065 protected void onUnauthorized(Response response) {
1069 createFolder.setHeader("X-Auth-Token", getToken());
1070 createFolder.setHeader("Accept", "*/*");
1071 createFolder.setHeader("Content-Length", "0");
1072 createFolder.setHeader("Content-Type", "application/folder");
1073 Scheduler.get().scheduleDeferred(createFolder);
1076 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1077 selectionModels.add(model);
1080 public OtherSharedTreeView getOtherSharedTreeView() {
1081 return otherSharedTreeView;
1084 public void updateTrash(boolean showFiles, Command callback) {
1085 updateFolder(trash, showFiles, callback);
1088 public void updateGroupsNode() {
1089 groupTreeView.updateGroupNode(null);
1092 public void addGroup(String groupname) {
1093 Group newGroup = new Group(groupname);
1094 account.addGroup(newGroup);
1095 groupTreeView.updateGroupNode(null);
1098 public void removeGroup(Group group) {
1099 account.removeGroup(group);
1103 public TreeView getSelectedTree() {
1104 return selectedTree;
1107 public Folder getSelection() {
1108 return selectedTree.getSelection();
1111 public void showFolderStatistics(int folderFileCount) {
1112 numOfFiles.setHTML(String.valueOf(folderFileCount));
1115 public GroupTreeView getGroupTreeView() {
1116 return groupTreeView;
1119 public void sessionExpired() {
1120 new SessionExpiredDialog(this).center();
1123 public void updateRootFolder(Command callback) {
1124 updateFolder(account.getPithos(), false, callback);
1127 void createMySharedTree() {
1128 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1129 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1131 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
1132 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1133 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1134 upload.setEnabled(false);
1135 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1139 selectionModels.add(mysharedTreeSelectionModel);
1140 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1141 mysharedTreeViewModel.initialize(new Command() {
1144 public void execute() {
1145 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1146 trees.insert(mysharedTreeView, 2);
1147 treeViews.add(mysharedTreeView);
1148 createOtherSharedTree();
1153 void createOtherSharedTree() {
1154 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1155 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1157 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
1158 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1159 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1160 otherSharedTreeView.addStyleName("cellTreeWidget-selectedTree");
1161 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1162 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1166 selectionModels.add(otherSharedTreeSelectionModel);
1167 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1168 otherSharedTreeViewModel.initialize(new Command() {
1171 public void execute() {
1172 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1173 trees.insert(otherSharedTreeView, 3);
1174 treeViews.add(otherSharedTreeView);
1179 public void logoff() {
1180 Configuration conf = (Configuration) GWT.create(Configuration.class);
1181 Cookies.removeCookie(conf.authCookie());
1182 Cookies.removeCookie(conf.authTokenCookie(), "/");
1183 for (String s: Cookies.getCookieNames())
1184 if (s.startsWith(conf.shibSessionCookiePrefix()))
1185 Cookies.removeCookie(s, "/");
1186 Window.Location.assign(Window.Location.getPath());