2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client;
37 import com.google.gwt.core.client.EntryPoint;
38 import com.google.gwt.core.client.GWT;
39 import com.google.gwt.core.client.JsArrayString;
40 import com.google.gwt.core.client.Scheduler;
41 import com.google.gwt.core.client.Scheduler.RepeatingCommand;
42 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
43 import com.google.gwt.event.dom.client.ClickEvent;
44 import com.google.gwt.event.dom.client.ClickHandler;
45 import com.google.gwt.event.logical.shared.ResizeEvent;
46 import com.google.gwt.event.logical.shared.ResizeHandler;
47 import com.google.gwt.http.client.Request;
48 import com.google.gwt.http.client.Response;
49 import com.google.gwt.http.client.URL;
50 import com.google.gwt.i18n.client.DateTimeFormat;
51 import com.google.gwt.i18n.client.Dictionary;
52 import com.google.gwt.i18n.client.TimeZone;
53 import com.google.gwt.json.client.JSONObject;
54 import com.google.gwt.resources.client.ClientBundle;
55 import com.google.gwt.resources.client.CssResource;
56 import com.google.gwt.resources.client.ImageResource;
57 import com.google.gwt.resources.client.ImageResource.ImageOptions;
58 import com.google.gwt.user.client.*;
59 import com.google.gwt.user.client.ui.*;
60 import com.google.gwt.view.client.SelectionChangeEvent;
61 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
62 import com.google.gwt.view.client.SingleSelectionModel;
63 import gr.grnet.pithos.web.client.catalog.GetUserCatalogs;
64 import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
65 import gr.grnet.pithos.web.client.catalog.UserCatalogs;
66 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
67 import gr.grnet.pithos.web.client.foldertree.*;
68 import gr.grnet.pithos.web.client.grouptree.Group;
69 import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
70 import gr.grnet.pithos.web.client.grouptree.GroupTreeViewModel;
71 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView;
72 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
73 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
74 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
75 import gr.grnet.pithos.web.client.rest.*;
76 import org.apache.http.HttpStatus;
81 * Entry point classes define <code>onModuleLoad()</code>.
83 public class Pithos implements EntryPoint, ResizeHandler {
85 public static final String HOME_CONTAINER = "pithos";
87 public static final String TRASH_CONTAINER = "trash";
89 public static final Configuration config = GWT.create(Configuration.class);
91 public interface Style extends CssResource {
92 String commandAnchor();
96 @ClassName("gwt-HTML")
101 String uploadAlertLink();
103 String uploadAlertProgress();
105 String uploadAlertPercent();
107 String uploadAlertClose();
110 public interface Resources extends ClientBundle {
111 @Source("Pithos.css")
114 @Source("gr/grnet/pithos/resources/close-popup.png")
115 ImageResource closePopup();
118 public static Resources resources = GWT.create(Resources.class);
121 * Instantiate an application-level image bundle. This object will provide
122 * programmatic access to all the images needed by widgets.
124 static Images images = (Images) GWT.create(Images.class);
126 public String getUserID() {
130 public UserCatalogs getUserCatalogs() {
134 public String getCurrentUserDisplayNameOrID() {
135 final String displayName = userCatalogs.getDisplayName(getUserID());
136 return displayName == null ? getUserID() : displayName;
139 public boolean hasUserDisplayNameForID(String userID) {
140 return userCatalogs.getDisplayName(userID) != null;
143 public String getUserDisplayNameForID(String userID) {
144 return userCatalogs.getDisplayName(userID);
147 public String getUserIDForDisplayName(String displayName) {
148 return userCatalogs.getUserID(displayName);
151 public List<String> getUserDisplayNamesForIDs(List<String> userIDs) {
152 if(userIDs == null) {
153 userIDs = new ArrayList<String>();
155 final List<String> userDisplayNames = new ArrayList<String>();
156 for(String userID : userIDs) {
157 final String displayName = getUserDisplayNameForID(userID);
158 userDisplayNames.add(displayName);
161 return userDisplayNames;
164 public List<String> filterUserIDsWithUnknownDisplayName(Collection<String> userIDs) {
165 if(userIDs == null) {
166 userIDs = new ArrayList<String>();
168 final List<String> filtered = new ArrayList<String>();
169 for(String userID : userIDs) {
170 if(!this.userCatalogs.hasID(userID)) {
171 filtered.add(userID);
177 public void setAccount(AccountResource acct) {
181 public AccountResource getAccount() {
185 public void updateFolder(Folder f, boolean showfiles, Command callback, final boolean openParent) {
186 folderTreeView.updateFolder(f, showfiles, callback, openParent);
189 public void updateGroupNode(Group group) {
190 groupTreeView.updateGroupNode(group);
193 public void updateMySharedRoot() {
194 mysharedTreeView.updateRoot();
197 public void updateSharedFolder(Folder f, boolean showfiles, Command callback) {
198 mysharedTreeView.updateFolder(f, showfiles, callback);
201 public void updateSharedFolder(Folder f, boolean showfiles) {
202 updateSharedFolder(f, showfiles, null);
205 public void updateOtherSharedFolder(Folder f, boolean showfiles, Command callback) {
206 otherSharedTreeView.updateFolder(f, showfiles, callback);
209 public MysharedTreeView getMySharedTreeView() {
210 return mysharedTreeView;
214 * An aggregate image bundle that pulls together all the images for this
215 * application into a single bundle.
217 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
219 @Source("gr/grnet/pithos/resources/document.png")
220 ImageResource folders();
222 @Source("gr/grnet/pithos/resources/advancedsettings.png")
223 @ImageOptions(width = 32, height = 32)
224 ImageResource tools();
227 private Throwable error;
230 * The Application Clipboard implementation;
232 private Clipboard clipboard = new Clipboard();
235 * The top panel that contains the menu bar.
237 private TopPanel topPanel;
240 * The panel that contains the various system messages.
242 private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
245 * The bottom panel that contains the status bar.
247 StatusPanel statusPanel = null;
250 * The file list widget.
252 private FileList fileList;
255 * The tab panel that occupies the right side of the screen.
257 private VerticalPanel inner = new VerticalPanel();
261 * The split panel that will contain the left and right panels.
263 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
266 * The currently selected item in the application, for use by the Edit menu
267 * commands. Potential types are Folder, File, User and Group.
269 private Object currentSelection;
271 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
274 * The ID that uniquely identifies the user in Pithos+.
275 * Currently this is a UUID. It used to be the user's email.
277 private String userID = null;
280 * Hold mappings from user UUIDs to emails and vice-versa.
282 private UserCatalogs userCatalogs = new UserCatalogs();
285 * The authentication token of the current user.
287 private String userToken;
291 SingleSelectionModel<Folder> folderTreeSelectionModel;
292 FolderTreeViewModel folderTreeViewModel;
293 FolderTreeView folderTreeView;
295 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
296 MysharedTreeViewModel mysharedTreeViewModel;
297 MysharedTreeView mysharedTreeView = null;
299 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
300 OtherSharedTreeViewModel otherSharedTreeViewModel;
301 OtherSharedTreeView otherSharedTreeView = null;
303 GroupTreeViewModel groupTreeViewModel;
304 GroupTreeView groupTreeView;
306 TreeView selectedTree;
307 protected AccountResource account;
311 List<Composite> treeViews = new ArrayList<Composite>();
313 @SuppressWarnings("rawtypes")
314 List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
316 public Button upload;
318 private HTML numOfFiles;
320 private Toolbar toolbar;
322 private FileUploadDialog fileUploadDialog = new FileUploadDialog(this);
324 UploadAlert uploadAlert;
329 public void onModuleLoad() {
330 if(parseUserCredentials()) {
335 private void initialize() {
336 System.out.println("initialize(), userToken = " + userToken);
337 lastModified = new Date(); //Initialize if-modified-since value with now.
338 resources.pithosCss().ensureInjected();
339 boolean bareContent = Window.Location.getParameter("noframe") != null;
340 String contentWidth = bareContent ? "100%" : "75%";
342 VerticalPanel outer = new VerticalPanel();
343 outer.setWidth("100%");
345 outer.addStyleName("pithos-outer");
349 topPanel = new TopPanel(this, Pithos.images);
350 topPanel.setWidth("100%");
352 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
355 messagePanel.setVisible(false);
356 outer.add(messagePanel);
357 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
358 outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
360 HorizontalPanel header = new HorizontalPanel();
361 header.addStyleName("pithos-header");
362 header.setWidth(contentWidth);
364 header.addStyleName("pithos-header-noframe");
366 upload = new Button("Upload", new ClickHandler() {
368 public void onClick(ClickEvent event) {
369 if(getSelection() != null) {
370 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
374 upload.addStyleName("pithos-uploadButton");
376 header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
377 header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
379 toolbar = new Toolbar(this);
381 header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
382 header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
384 HorizontalPanel folderStatistics = new HorizontalPanel();
385 folderStatistics.addStyleName("pithos-folderStatistics");
386 numOfFiles = new HTML();
387 folderStatistics.add(numOfFiles);
388 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
389 HTML numOfFilesLabel = new HTML(" Files");
390 folderStatistics.add(numOfFilesLabel);
391 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
392 header.add(folderStatistics);
393 header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
394 header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
395 header.setCellWidth(folderStatistics, "40px");
397 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
398 // Inner contains the various lists
399 inner.sinkEvents(Event.ONCONTEXTMENU);
400 inner.setWidth("100%");
402 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
403 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
405 public void onSelectionChange(SelectionChangeEvent event) {
406 if(folderTreeSelectionModel.getSelectedObject() != null) {
407 deselectOthers(folderTreeView, folderTreeSelectionModel);
408 applyPermissions(folderTreeSelectionModel.getSelectedObject());
409 Folder f = folderTreeSelectionModel.getSelectedObject();
410 updateFolder(f, true, new Command() {
413 public void execute() {
417 showRelevantToolbarButtons();
420 if(getSelectedTree().equals(folderTreeView)) {
421 setSelectedTree(null);
423 if(getSelectedTree() == null) {
424 showRelevantToolbarButtons();
429 selectionModels.add(folderTreeSelectionModel);
431 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
432 folderTreeView = new FolderTreeView(folderTreeViewModel);
433 treeViews.add(folderTreeView);
435 fileList = new FileList(this, images);
438 trees = new VerticalPanel();
439 trees.setWidth("100%");
441 // Add the left and right panels to the split panel.
442 splitPanel.setLeftWidget(trees);
443 FlowPanel right = new FlowPanel();
444 right.getElement().setId("rightPanel");
446 splitPanel.setRightWidget(right);
447 splitPanel.setSplitPosition("219px");
448 splitPanel.setSize("100%", "100%");
449 splitPanel.addStyleName("pithos-splitPanel");
450 splitPanel.setWidth(contentWidth);
451 outer.add(splitPanel);
452 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
455 statusPanel = new StatusPanel();
456 statusPanel.setWidth("100%");
457 outer.add(statusPanel);
458 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
461 splitPanel.addStyleName("pithos-splitPanel-noframe");
464 // Hook the window resize event, so that we can adjust the UI.
465 Window.addResizeHandler(this);
466 // Clear out the window's built-in margin, because we want to take
467 // advantage of the entire client area.
468 Window.setMargin("0px");
469 // Finally, add the outer panel to the RootPanel, so that it will be
471 RootPanel.get().add(outer);
472 // Call the window resized handler to get the initial sizes setup. Doing
473 // this in a deferred command causes it to occur after all widgets'
474 // sizes have been computed by the browser.
475 Scheduler.get().scheduleIncremental(new RepeatingCommand() {
478 public boolean execute() {
479 if(!isCloudbarReady()) {
482 onWindowResized(Window.getClientHeight());
487 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
489 public void execute() {
490 fetchAccount(new Command() {
493 public void execute() {
494 if(!account.hasHomeContainer()) {
495 createHomeContainer(account, this);
497 else if(!account.hasTrashContainer()) {
498 createTrashContainer(this);
501 for(Folder f : account.getContainers()) {
502 if(f.getName().equals(Pithos.TRASH_CONTAINER)) {
507 trees.add(folderTreeView);
508 folderTreeViewModel.initialize(account, new Command() {
511 public void execute() {
512 createMySharedTree();
516 HorizontalPanel separator = new HorizontalPanel();
517 separator.addStyleName("pithos-statisticsSeparator");
518 separator.add(new HTML(""));
519 trees.add(separator);
521 groupTreeViewModel = new GroupTreeViewModel(Pithos.this);
522 groupTreeView = new GroupTreeView(groupTreeViewModel);
523 treeViews.add(groupTreeView);
524 trees.add(groupTreeView);
525 folderTreeView.showStatistics(account);
533 public void scheduleResfresh() {
534 Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
537 public boolean execute() {
538 final Folder f = getSelection();
543 HeadRequest<Folder> head = new HeadRequest<Folder>(Folder.class, getApiPath(), f.getOwnerID(), "/" + f.getContainer()) {
546 public void onSuccess(Folder _result) {
547 lastModified = new Date();
548 if(getSelectedTree().equals(folderTreeView)) {
549 updateFolder(f, true, new Command() {
552 public void execute() {
558 else if(getSelectedTree().equals(mysharedTreeView)) {
559 updateSharedFolder(f, true, new Command() {
562 public void execute() {
573 public void onError(Throwable t) {
574 if(t instanceof RestException && ((RestException) t).getHttpStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
577 else if(retries >= MAX_RETRIES) {
578 GWT.log("Error heading folder", t);
580 if(t instanceof RestException) {
581 displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
584 displayError("System error heading folder: " + t.getMessage());
588 GWT.log("Retry " + retries);
589 Scheduler.get().scheduleDeferred(this);
594 protected void onUnauthorized(Response response) {
595 if(retries >= MAX_RETRIES) {
600 Scheduler.get().scheduleDeferred(this);
604 head.setHeader("X-Auth-Token", getUserToken());
605 head.setHeader("If-Modified-Since", DateTimeFormat.getFormat("EEE, dd MMM yyyy HH:mm:ss").format(lastModified, TimeZone.createTimeZone(0)) + " GMT");
606 Scheduler.get().scheduleDeferred(head);
613 public void applyPermissions(Folder f) {
616 upload.setEnabled(false);
620 Boolean[] perms = f.getPermissions().get(userID);
621 if(f.getOwnerID().equals(userID) || (perms != null && perms[1] != null && perms[1])) {
622 upload.setEnabled(true);
626 upload.setEnabled(false);
632 upload.setEnabled(false);
637 @SuppressWarnings({"rawtypes", "unchecked"})
638 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
639 selectedTree = _selectedTree;
641 for(SingleSelectionModel s : selectionModels) {
642 if(!s.equals(model) && s.getSelectedObject() != null) {
643 s.setSelected(s.getSelectedObject(), false);
648 public void showFiles(final Folder f) {
649 Set<File> files = f.getFiles();
653 public void showFiles(Set<File> files) {
654 fileList.setFiles(new ArrayList<File>(files));
658 * Parse and store the user credentials to the appropriate fields.
660 private boolean parseUserCredentials() {
661 Configuration conf = (Configuration) GWT.create(Configuration.class);
662 Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
663 String cookie = otherProperties.get("authCookie");
664 String auth = Cookies.getCookie(cookie);
669 if(auth.startsWith("\"")) {
670 auth = auth.substring(1);
672 if(auth.endsWith("\"")) {
673 auth = auth.substring(0, auth.length() - 1);
675 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
676 if(authSplit.length != 2) {
680 userID = authSplit[0];
681 userToken = authSplit[1];
683 String gotoUrl = Window.Location.getParameter("goto");
684 if(gotoUrl != null && gotoUrl.length() > 0) {
685 Window.Location.assign(gotoUrl);
692 * Redirect the user to the login page for authentication.
694 protected void authenticateUser() {
695 Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
696 Window.Location.assign(otherProperties.get("loginUrl") + Window.Location.getHref());
699 public void fetchAccount(final Command callback) {
700 String path = "?format=json";
702 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), userID, path) {
704 public void onSuccess(AccountResource _result) {
706 if(callback != null) {
709 // Initialize the user catalog
710 new UpdateUserCatalogs(Pithos.this, Pithos.this.getUserID()).scheduleDeferred();
714 public void onError(Throwable t) {
715 GWT.log("Error getting account", t);
717 if(t instanceof RestException) {
718 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
721 displayError("System error fetching user data: " + t.getMessage());
726 protected void onUnauthorized(Response response) {
730 getAccount.setHeader("X-Auth-Token", userToken);
731 Scheduler.get().scheduleDeferred(getAccount);
734 public void updateStatistics() {
735 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), userID, "", account) {
738 public void onSuccess(AccountResource _result) {
739 folderTreeView.showStatistics(account);
743 public void onError(Throwable t) {
744 GWT.log("Error getting account", t);
746 if(t instanceof RestException) {
747 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
750 displayError("System error fetching user data: " + t.getMessage());
755 protected void onUnauthorized(Response response) {
759 headAccount.setHeader("X-Auth-Token", userToken);
760 Scheduler.get().scheduleDeferred(headAccount);
763 protected void createHomeContainer(final AccountResource _account, final Command callback) {
764 String path = "/" + Pithos.HOME_CONTAINER;
765 PutRequest createPithos = new PutRequest(getApiPath(), getUserID(), path) {
767 public void onSuccess(Resource result) {
768 if(!_account.hasTrashContainer()) {
769 createTrashContainer(callback);
772 fetchAccount(callback);
777 public void onError(Throwable t) {
778 GWT.log("Error creating pithos", t);
780 if(t instanceof RestException) {
781 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
784 displayError("System error Error creating pithos: " + t.getMessage());
789 protected void onUnauthorized(Response response) {
793 createPithos.setHeader("X-Auth-Token", getUserToken());
794 Scheduler.get().scheduleDeferred(createPithos);
797 protected void createTrashContainer(final Command callback) {
798 String path = "/" + Pithos.TRASH_CONTAINER;
799 PutRequest createPithos = new PutRequest(getApiPath(), getUserID(), path) {
801 public void onSuccess(Resource result) {
802 fetchAccount(callback);
806 public void onError(Throwable t) {
807 GWT.log("Error creating pithos", t);
809 if(t instanceof RestException) {
810 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
813 displayError("System error Error creating pithos: " + t.getMessage());
818 protected void onUnauthorized(Response response) {
822 createPithos.setHeader("X-Auth-Token", getUserToken());
823 Scheduler.get().scheduleDeferred(createPithos);
827 * Creates an HTML fragment that places an image & caption together, for use
830 * @param imageProto an image prototype for an image
831 * @param caption the group caption
832 * @return the header HTML fragment
834 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
835 String captionHTML = "<table class='caption' cellpadding='0' "
836 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
837 + "</td><td id =" + caption + " class='rcaption'><b style='white-space:nowrap'> "
838 + caption + "</b></td></tr></table>";
842 protected void onWindowResized(int height) {
843 // Adjust the split panel to take up the available room in the window.
844 int newHeight = height - splitPanel.getAbsoluteTop() - 153;
848 splitPanel.setHeight("" + newHeight);
849 inner.setHeight("" + newHeight);
852 native boolean isCloudbarReady()/*-{
853 if ($wnd.$("div.cloudbar") && $wnd.$("div.cloudbar").height() > 0)
859 public void onResize(ResizeEvent event) {
860 int height = event.getHeight();
861 onWindowResized(height);
865 * Display an error message.
867 * @param msg the message to display
869 public void displayError(String msg) {
870 messagePanel.displayError(msg);
871 onWindowResized(Window.getClientHeight());
875 * Display a warning message.
877 * @param msg the message to display
879 public void displayWarning(String msg) {
880 messagePanel.displayWarning(msg);
881 onWindowResized(Window.getClientHeight());
885 * Display an informational message.
887 * @param msg the message to display
889 public void displayInformation(String msg) {
890 messagePanel.displayInformation(msg);
891 onWindowResized(Window.getClientHeight());
895 * Retrieve the fileList.
897 * @return the fileList
899 public FileList getFileList() {
904 * Retrieve the topPanel.
906 * @return the topPanel
908 TopPanel getTopPanel() {
913 * Retrieve the clipboard.
915 * @return the clipboard
917 public Clipboard getClipboard() {
921 public StatusPanel getStatusPanel() {
925 public String getUserToken() {
929 public static native void preventIESelection() /*-{
930 $doc.body.onselectstart = function () {
935 public static native void enableIESelection() /*-{
936 if ($doc.body.onselectstart != null)
937 $doc.body.onselectstart = null;
941 * @return the absolute path of the API root URL
943 public String getApiPath() {
944 Configuration conf = (Configuration) GWT.create(Configuration.class);
945 return conf.apiPath();
949 * History support for folder navigation
950 * adds a new browser history entry
954 public void updateHistory(String key) {
955 // Replace any whitespace of the initial string to "+"
956 // String result = key.replaceAll("\\s","+");
957 // Add a new browser history entry.
958 // History.newItem(result);
959 History.newItem(key);
962 public void deleteFolder(final Folder folder, final Command callback) {
963 final PleaseWaitPopup pwp = new PleaseWaitPopup();
965 String path = "/" + folder.getContainer() + "/" + folder.getPrefix() + "?delimiter=/" + "&t=" + System.currentTimeMillis();
966 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), folder.getOwnerID(), path) {
969 protected void onUnauthorized(Response response) {
975 public void onSuccess(Resource result) {
976 updateFolder(folder.getParent(), true, new Command() {
979 public void execute() {
980 folderTreeSelectionModel.setSelected(folder.getParent(), true);
982 if(callback != null) {
991 public void onError(Throwable t) {
994 if(t instanceof RestException) {
995 if(((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND) {
996 displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText());
1003 displayError("System error unable to delete folder: " + t.getMessage());
1008 deleteFolder.setHeader("X-Auth-Token", getUserToken());
1009 Scheduler.get().scheduleDeferred(deleteFolder);
1012 public FolderTreeView getFolderTreeView() {
1013 return folderTreeView;
1016 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
1017 if(iter.hasNext()) {
1018 File file = iter.next();
1019 String path = targetUri + "/" + file.getName();
1020 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
1022 public void onSuccess(Resource result) {
1023 copyFiles(iter, targetUsername, targetUri, callback);
1027 public void onError(Throwable t) {
1030 if(t instanceof RestException) {
1031 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
1034 displayError("System error unable to copy file: " + t.getMessage());
1039 protected void onUnauthorized(Response response) {
1043 copyFile.setHeader("X-Auth-Token", getUserToken());
1044 copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
1045 if(!file.getOwnerID().equals(targetUsername)) {
1046 copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwnerID()));
1048 copyFile.setHeader("Content-Type", file.getContentType());
1049 Scheduler.get().scheduleDeferred(copyFile);
1051 else if(callback != null) {
1056 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, boolean move, final Command callback) {
1057 String path = targetUri + "?delimiter=/";
1058 PutRequest copyFolder = new PutRequest(getApiPath(), targetUsername, path) {
1060 public void onSuccess(Resource result) {
1061 if(callback != null) {
1067 public void onError(Throwable t) {
1070 if(t instanceof RestException) {
1071 displayError("Unable to copy folder: " + ((RestException) t).getHttpStatusText());
1074 displayError("System error copying folder: " + t.getMessage());
1079 protected void onUnauthorized(Response response) {
1083 copyFolder.setHeader("X-Auth-Token", getUserToken());
1084 copyFolder.setHeader("Accept", "*/*");
1085 copyFolder.setHeader("Content-Length", "0");
1086 copyFolder.setHeader("Content-Type", "application/directory");
1087 if(!f.getOwnerID().equals(targetUsername)) {
1088 copyFolder.setHeader("X-Source-Account", f.getOwnerID());
1091 copyFolder.setHeader("X-Move-From", URL.encodePathSegment(f.getUri()));
1094 copyFolder.setHeader("X-Copy-From", URL.encodePathSegment(f.getUri()));
1096 Scheduler.get().scheduleDeferred(copyFolder);
1099 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1100 selectionModels.add(model);
1103 public OtherSharedTreeView getOtherSharedTreeView() {
1104 return otherSharedTreeView;
1107 public void updateTrash(boolean showFiles, Command callback) {
1108 updateFolder(trash, showFiles, callback, true);
1111 public void updateGroupsNode() {
1112 groupTreeView.updateGroupNode(null);
1115 public Group addGroup(String groupname) {
1116 Group newGroup = new Group(groupname);
1117 account.addGroup(newGroup);
1118 groupTreeView.updateGroupNode(null);
1122 public void removeGroup(Group group) {
1123 account.removeGroup(group);
1127 public TreeView getSelectedTree() {
1128 return selectedTree;
1131 public void setSelectedTree(TreeView selected) {
1132 selectedTree = selected;
1135 public Folder getSelection() {
1136 if(selectedTree != null) {
1137 return selectedTree.getSelection();
1142 public void showFolderStatistics(int folderFileCount) {
1143 numOfFiles.setHTML(String.valueOf(folderFileCount));
1146 public GroupTreeView getGroupTreeView() {
1147 return groupTreeView;
1150 public void sessionExpired() {
1151 new SessionExpiredDialog(this).center();
1154 public void updateRootFolder(Command callback) {
1155 updateFolder(account.getPithos(), false, callback, true);
1158 void createMySharedTree() {
1159 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1160 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1162 public void onSelectionChange(SelectionChangeEvent event) {
1163 if(mysharedTreeSelectionModel.getSelectedObject() != null) {
1164 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1165 upload.setEnabled(false);
1166 disableUploadArea();
1167 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1168 showRelevantToolbarButtons();
1171 if(getSelectedTree().equals(mysharedTreeView)) {
1172 setSelectedTree(null);
1174 if(getSelectedTree() == null) {
1175 showRelevantToolbarButtons();
1180 selectionModels.add(mysharedTreeSelectionModel);
1181 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1182 mysharedTreeViewModel.initialize(new Command() {
1185 public void execute() {
1186 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1187 trees.insert(mysharedTreeView, 2);
1188 treeViews.add(mysharedTreeView);
1189 createOtherSharedTree();
1194 void createOtherSharedTree() {
1195 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1196 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1198 public void onSelectionChange(SelectionChangeEvent event) {
1199 if(otherSharedTreeSelectionModel.getSelectedObject() != null) {
1200 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1201 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1202 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true, null);
1203 showRelevantToolbarButtons();
1206 if(getSelectedTree().equals(otherSharedTreeView)) {
1207 setSelectedTree(null);
1209 if(getSelectedTree() == null) {
1210 showRelevantToolbarButtons();
1215 selectionModels.add(otherSharedTreeSelectionModel);
1216 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1217 otherSharedTreeViewModel.initialize(new Command() {
1220 public void execute() {
1221 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1222 trees.insert(otherSharedTreeView, 1);
1223 treeViews.add(otherSharedTreeView);
1229 public native void log1(String message)/*-{
1230 $wnd.console.log(message);
1233 public String getErrorData() {
1235 return error.toString();
1240 public void setError(Throwable t) {
1244 public void showRelevantToolbarButtons() {
1245 toolbar.showRelevantButtons();
1248 public FileUploadDialog getFileUploadDialog() {
1249 if(fileUploadDialog == null) {
1250 fileUploadDialog = new FileUploadDialog(this);
1252 return fileUploadDialog;
1255 public void hideUploadIndicator() {
1256 upload.removeStyleName("pithos-uploadButton-loading");
1257 upload.setTitle("");
1260 public void showUploadIndicator() {
1261 upload.addStyleName("pithos-uploadButton-loading");
1262 upload.setTitle("Upload in progress. Click for details.");
1265 public void scheduleFolderHeadCommand(final Folder folder, final Command callback) {
1266 if(folder == null) {
1267 if(callback != null) {
1272 HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, getApiPath(), folder.getOwnerID(), folder.getUri(), folder) {
1275 public void onSuccess(Folder _result) {
1276 if(callback != null) {
1282 public void onError(Throwable t) {
1283 if(t instanceof RestException) {
1284 if(((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
1285 final String path = folder.getUri();
1286 PutRequest newFolder = new PutRequest(getApiPath(), folder.getOwnerID(), path) {
1288 public void onSuccess(Resource _result) {
1289 scheduleFolderHeadCommand(folder, callback);
1293 public void onError(Throwable _t) {
1296 if(_t instanceof RestException) {
1297 displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
1300 displayError("System error creating folder: " + _t.getMessage());
1305 protected void onUnauthorized(Response response) {
1309 newFolder.setHeader("X-Auth-Token", getUserToken());
1310 newFolder.setHeader("Content-Type", "application/folder");
1311 newFolder.setHeader("Accept", "*/*");
1312 newFolder.setHeader("Content-Length", "0");
1313 Scheduler.get().scheduleDeferred(newFolder);
1315 else if(((RestException) t).getHttpStatusCode() == Response.SC_FORBIDDEN) {
1319 displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
1323 displayError("System error heading folder: " + t.getMessage());
1326 GWT.log("Error heading folder", t);
1331 protected void onUnauthorized(Response response) {
1335 headFolder.setHeader("X-Auth-Token", getUserToken());
1336 Scheduler.get().scheduleDeferred(headFolder);
1340 public void scheduleFileHeadCommand(File f, final Command callback) {
1341 HeadRequest<File> headFile = new HeadRequest<File>(File.class, getApiPath(), f.getOwnerID(), f.getUri(), f) {
1344 public void onSuccess(File _result) {
1345 if(callback != null) {
1351 public void onError(Throwable t) {
1352 GWT.log("Error heading file", t);
1354 if(t instanceof RestException) {
1355 displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
1358 displayError("System error heading file: " + t.getMessage());
1363 protected void onUnauthorized(Response response) {
1367 headFile.setHeader("X-Auth-Token", getUserToken());
1368 Scheduler.get().scheduleDeferred(headFile);
1371 public boolean isMySharedSelected() {
1372 return getSelectedTree().equals(getMySharedTreeView());
1375 private Folder getUploadFolder() {
1376 if(folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1377 return getSelection();
1382 private void updateUploadFolder() {
1383 updateUploadFolder(null);
1386 private void updateUploadFolder(final JsArrayString urls) {
1387 if(folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1388 Folder f = getSelection();
1389 if(getSelectedTree().equals(getFolderTreeView())) {
1390 updateFolder(f, true, new Command() {
1393 public void execute() {
1396 selectUploadedFiles(urls);
1402 updateOtherSharedFolder(f, true, null);
1407 public native void disableUploadArea() /*-{
1408 var uploader = $wnd.$("#uploader").pluploadQueue();
1409 var dropElm = $wnd.document.getElementById('rightPanel');
1410 $wnd.plupload.removeAllEvents(dropElm, uploader.id);
1413 public native void enableUploadArea() /*-{
1414 var uploader = $wnd.$("#uploader").pluploadQueue();
1415 var dropElm = $wnd.document.getElementById('rightPanel');
1416 $wnd.plupload.removeAllEvents(dropElm, uploader.id);
1417 if (uploader.runtime == 'html5') {
1418 uploader.settings.drop_element = 'rightPanel';
1419 uploader.trigger('PostInit');
1423 public void showUploadAlert(int nOfFiles) {
1424 if(uploadAlert == null) {
1425 uploadAlert = new UploadAlert(this, nOfFiles);
1427 if(!uploadAlert.isShowing()) {
1428 uploadAlert.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
1431 public void setPosition(int offsetWidth, int offsetHeight) {
1432 uploadAlert.setPopupPosition((Window.getClientWidth() - offsetWidth) / 2, statusPanel.getAbsoluteTop() - offsetHeight);
1436 uploadAlert.setNumOfFiles(nOfFiles);
1439 public void hideUploadAlert() {
1440 if(uploadAlert != null && uploadAlert.isShowing()) {
1445 public void selectUploadedFiles(JsArrayString urls) {
1446 List<String> selectedUrls = new ArrayList<String>();
1447 for(int i = 0; i < urls.length(); i++) {
1448 selectedUrls.add(urls.get(i));
1450 fileList.selectByUrl(selectedUrls);
1453 public void emptyContainer(final Folder container) {
1454 String path = "/" + container.getName() + "?delimiter=/";
1455 DeleteRequest delete = new DeleteRequest(getApiPath(), getUserID(), path) {
1458 protected void onUnauthorized(Response response) {
1463 public void onSuccess(Resource result) {
1464 updateFolder(container, true, null, true);
1468 public void onError(Throwable t) {
1469 GWT.log("Error deleting trash", t);
1471 if(t instanceof RestException) {
1472 displayError("Error deleting trash: " + ((RestException) t).getHttpStatusText());
1475 displayError("System error deleting trash: " + t.getMessage());
1479 delete.setHeader("X-Auth-Token", getUserToken());
1480 Scheduler.get().scheduleDeferred(delete);