2 * Copyright 2011-2013 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.Response;
48 import com.google.gwt.http.client.URL;
49 import com.google.gwt.i18n.client.DateTimeFormat;
50 import com.google.gwt.i18n.client.Dictionary;
51 import com.google.gwt.i18n.client.TimeZone;
52 import com.google.gwt.resources.client.ClientBundle;
53 import com.google.gwt.resources.client.CssResource;
54 import com.google.gwt.resources.client.ImageResource;
55 import com.google.gwt.resources.client.ImageResource.ImageOptions;
56 import com.google.gwt.user.client.*;
57 import com.google.gwt.user.client.ui.*;
58 import com.google.gwt.view.client.SelectionChangeEvent;
59 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
60 import com.google.gwt.view.client.SingleSelectionModel;
61 import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
62 import gr.grnet.pithos.web.client.catalog.UserCatalogs;
63 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
64 import gr.grnet.pithos.web.client.foldertree.*;
65 import gr.grnet.pithos.web.client.grouptree.Group;
66 import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
67 import gr.grnet.pithos.web.client.grouptree.GroupTreeViewModel;
68 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView;
69 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
70 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
71 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
72 import gr.grnet.pithos.web.client.rest.*;
73 import org.apache.http.HttpStatus;
78 * Entry point classes define <code>onModuleLoad()</code>.
80 public class Pithos implements EntryPoint, ResizeHandler {
81 private static final boolean IsLOGEnabled = false;
82 public static final boolean IsDetailedHTTPLOGEnabled = true;
83 public static final boolean IsFullResponseBodyLOGEnabled = true;
85 public static final Set<String> HTTPHeadersToIgnoreInLOG = new HashSet<String>();
87 HTTPHeadersToIgnoreInLOG.add(Const.HTTP_HEADER_CONNECTION);
88 HTTPHeadersToIgnoreInLOG.add(Const.HTTP_HEADER_DATE);
89 HTTPHeadersToIgnoreInLOG.add(Const.HTTP_HEADER_KEEP_ALIVE);
90 HTTPHeadersToIgnoreInLOG.add(Const.HTTP_HEADER_SERVER);
91 HTTPHeadersToIgnoreInLOG.add(Const.HTTP_HEADER_VARY);
92 HTTPHeadersToIgnoreInLOG.add(Const.IF_MODIFIED_SINCE);
95 public static final Configuration config = GWT.create(Configuration.class);
97 public interface Style extends CssResource {
98 String commandAnchor();
102 @ClassName("gwt-HTML")
105 String uploadAlert();
107 String uploadAlertLink();
109 String uploadAlertProgress();
111 String uploadAlertPercent();
113 String uploadAlertClose();
116 public interface Resources extends ClientBundle {
117 @Source("Pithos.css")
120 @Source("gr/grnet/pithos/resources/close-popup.png")
121 ImageResource closePopup();
124 public static Resources resources = GWT.create(Resources.class);
127 * Instantiate an application-level image bundle. This object will provide
128 * programmatic access to all the images needed by widgets.
130 static Images images = (Images) GWT.create(Images.class);
132 public String getUserID() {
136 public UserCatalogs getUserCatalogs() {
140 public String getCurrentUserDisplayNameOrID() {
141 final String displayName = userCatalogs.getDisplayName(getUserID());
142 return displayName == null ? getUserID() : displayName;
145 public boolean hasDisplayNameForUserID(String userID) {
146 return userCatalogs.getDisplayName(userID) != null;
149 public boolean hasIDForUserDisplayName(String userDisplayName) {
150 return userCatalogs.getID(userDisplayName) != null;
153 public String getDisplayNameForUserID(String userID) {
154 return userCatalogs.getDisplayName(userID);
157 public String getIDForUserDisplayName(String userDisplayName) {
158 return userCatalogs.getID(userDisplayName);
161 public List<String> getDisplayNamesForUserIDs(List<String> userIDs) {
162 if(userIDs == null) {
163 userIDs = new ArrayList<String>();
165 final List<String> userDisplayNames = new ArrayList<String>();
166 for(String userID : userIDs) {
167 final String displayName = getDisplayNameForUserID(userID);
168 userDisplayNames.add(displayName);
171 return userDisplayNames;
174 public List<String> filterUserIDsWithUnknownDisplayName(Collection<String> userIDs) {
175 if(userIDs == null) {
176 userIDs = new ArrayList<String>();
178 final List<String> filtered = new ArrayList<String>();
179 for(String userID : userIDs) {
180 if(!this.userCatalogs.hasID(userID)) {
181 filtered.add(userID);
187 public void setAccount(AccountResource acct) {
191 public AccountResource getAccount() {
195 public void updateFolder(Folder f, boolean showfiles, Command callback, final boolean openParent) {
196 folderTreeView.updateFolder(f, showfiles, callback, openParent);
199 public void updateGroupNode(Group group) {
200 groupTreeView.updateGroupNode(group);
203 public void updateMySharedRoot() {
204 mysharedTreeView.updateRoot();
207 public void updateSharedFolder(Folder f, boolean showfiles, Command callback) {
208 mysharedTreeView.updateFolder(f, showfiles, callback);
211 public void updateSharedFolder(Folder f, boolean showfiles) {
212 updateSharedFolder(f, showfiles, null);
215 public void updateOtherSharedFolder(Folder f, boolean showfiles, Command callback) {
216 otherSharedTreeView.updateFolder(f, showfiles, callback);
219 public MysharedTreeView getMySharedTreeView() {
220 return mysharedTreeView;
224 * An aggregate image bundle that pulls together all the images for this
225 * application into a single bundle.
227 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
229 @Source("gr/grnet/pithos/resources/document.png")
230 ImageResource folders();
232 @Source("gr/grnet/pithos/resources/advancedsettings.png")
233 @ImageOptions(width = 32, height = 32)
234 ImageResource tools();
237 private Throwable error;
240 * The Application Clipboard implementation;
242 private Clipboard clipboard = new Clipboard();
245 * The top panel that contains the menu bar.
247 private TopPanel topPanel;
250 * The panel that contains the various system messages.
252 private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
255 * The bottom panel that contains the status bar.
257 StatusPanel statusPanel = null;
260 * The file list widget.
262 private FileList fileList;
265 * The tab panel that occupies the right side of the screen.
267 private VerticalPanel inner = new VerticalPanel();
271 * The split panel that will contain the left and right panels.
273 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
276 * The currently selected item in the application, for use by the Edit menu
277 * commands. Potential types are Folder, File, User and Group.
279 private Object currentSelection;
281 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
284 * The ID that uniquely identifies the user in Pithos+.
285 * Currently this is a UUID. It used to be the user's email.
287 private String userID = null;
290 * Hold mappings from user UUIDs to emails and vice-versa.
292 private UserCatalogs userCatalogs = new UserCatalogs();
295 * The authentication token of the current user.
297 private String userToken;
301 SingleSelectionModel<Folder> folderTreeSelectionModel;
302 FolderTreeViewModel folderTreeViewModel;
303 FolderTreeView folderTreeView;
305 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
306 MysharedTreeViewModel mysharedTreeViewModel;
307 MysharedTreeView mysharedTreeView = null;
309 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
310 OtherSharedTreeViewModel otherSharedTreeViewModel;
311 OtherSharedTreeView otherSharedTreeView = null;
313 GroupTreeViewModel groupTreeViewModel;
314 GroupTreeView groupTreeView;
316 TreeView selectedTree;
317 protected AccountResource account;
321 List<Composite> treeViews = new ArrayList<Composite>();
323 @SuppressWarnings("rawtypes")
324 List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
326 public Button upload;
328 private HTML numOfFiles;
330 private Toolbar toolbar;
332 private FileUploadDialog fileUploadDialog = new FileUploadDialog(this);
334 UploadAlert uploadAlert;
339 public void onModuleLoad() {
340 if(parseUserCredentials()) {
345 static native void __ConsoleLog(String message) /*-{
346 try { console.log(message); } catch (e) {}
349 public static void LOGError(Throwable error, StringBuilder sb) {
350 if(!isLOGEnabled()) { return; }
352 sb.append("\nException: [" + error.toString().replace("\n", "\n ") + "]");
353 Throwable cause = error.getCause();
355 sb.append("\nCauses:\n");
356 while(cause != null) {
358 sb.append("[" + cause.toString().replace("\n", "\n ") + "]");
360 cause = cause.getCause();
367 StackTraceElement[] stackTrace = error.getStackTrace();
368 sb.append("Stack trace (" + stackTrace.length + " elements):\n");
369 for(int i = 0; i < stackTrace.length; i++) {
370 StackTraceElement errorElem = stackTrace[i];
371 sb.append(" [" + i + "] ");
372 sb.append(errorElem.toString());
377 public static void LOGError(Throwable error) {
378 if(!isLOGEnabled()) { return; }
380 final StringBuilder sb = new StringBuilder();
382 if(sb.length() > 0) {
383 __ConsoleLog(sb.toString());
387 public static boolean isLOGEnabled() {
391 public static void LOG(Object ...args) {
392 if(!isLOGEnabled()) { return; }
394 final StringBuilder sb = new StringBuilder();
395 for(Object arg : args) {
396 if(arg instanceof Throwable) {
397 LOGError((Throwable) arg, sb);
404 if(sb.length() > 0) {
405 __ConsoleLog(sb.toString());
409 private void initialize() {
410 userCatalogs.updateWithIDAndName("*", "All Pithos users");
412 lastModified = new Date(); //Initialize if-modified-since value with now.
413 resources.pithosCss().ensureInjected();
414 boolean bareContent = Window.Location.getParameter("noframe") != null;
415 String contentWidth = bareContent ? Const.PERCENT_100 : Const.PERCENT_75;
417 VerticalPanel outer = new VerticalPanel();
418 outer.setWidth(Const.PERCENT_100);
420 outer.addStyleName("pithos-outer");
424 topPanel = new TopPanel(this, Pithos.images);
425 topPanel.setWidth(Const.PERCENT_100);
427 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
430 messagePanel.setVisible(false);
431 outer.add(messagePanel);
432 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
433 outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
435 HorizontalPanel header = new HorizontalPanel();
436 header.addStyleName("pithos-header");
437 header.setWidth(contentWidth);
439 header.addStyleName("pithos-header-noframe");
441 upload = new Button("Upload", new ClickHandler() {
443 public void onClick(ClickEvent event) {
444 if(getSelection() != null) {
445 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
449 upload.addStyleName("pithos-uploadButton");
451 header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
452 header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
454 toolbar = new Toolbar(this);
456 header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
457 header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
459 HorizontalPanel folderStatistics = new HorizontalPanel();
460 folderStatistics.addStyleName("pithos-folderStatistics");
461 numOfFiles = new HTML();
462 folderStatistics.add(numOfFiles);
463 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
464 HTML numOfFilesLabel = new HTML(" Files");
465 folderStatistics.add(numOfFilesLabel);
466 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
467 header.add(folderStatistics);
468 header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
469 header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
470 header.setCellWidth(folderStatistics, "40px");
472 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
473 // Inner contains the various lists
474 inner.sinkEvents(Event.ONCONTEXTMENU);
475 inner.setWidth(Const.PERCENT_100);
477 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
478 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
480 public void onSelectionChange(SelectionChangeEvent event) {
481 if(folderTreeSelectionModel.getSelectedObject() != null) {
482 deselectOthers(folderTreeView, folderTreeSelectionModel);
483 applyPermissions(folderTreeSelectionModel.getSelectedObject());
484 Folder f = folderTreeSelectionModel.getSelectedObject();
485 updateFolder(f, true, new Command() {
488 public void execute() {
492 showRelevantToolbarButtons();
495 if(getSelectedTree().equals(folderTreeView)) {
496 setSelectedTree(null);
498 if(getSelectedTree() == null) {
499 showRelevantToolbarButtons();
504 selectionModels.add(folderTreeSelectionModel);
506 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
507 folderTreeView = new FolderTreeView(folderTreeViewModel);
508 treeViews.add(folderTreeView);
510 fileList = new FileList(this, images);
513 trees = new VerticalPanel();
514 trees.setWidth(Const.PERCENT_100);
516 // Add the left and right panels to the split panel.
517 splitPanel.setLeftWidget(trees);
518 FlowPanel right = new FlowPanel();
519 right.getElement().setId("rightPanel");
521 splitPanel.setRightWidget(right);
522 splitPanel.setSplitPosition("219px");
523 splitPanel.setSize(Const.PERCENT_100, Const.PERCENT_100);
524 splitPanel.addStyleName("pithos-splitPanel");
525 splitPanel.setWidth(contentWidth);
526 outer.add(splitPanel);
527 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
530 statusPanel = new StatusPanel();
531 statusPanel.setWidth(Const.PERCENT_100);
532 outer.add(statusPanel);
533 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
536 splitPanel.addStyleName("pithos-splitPanel-noframe");
539 // Hook the window resize event, so that we can adjust the UI.
540 Window.addResizeHandler(this);
541 // Clear out the window's built-in margin, because we want to take
542 // advantage of the entire client area.
543 Window.setMargin("0px");
544 // Finally, add the outer panel to the RootPanel, so that it will be
546 RootPanel.get().add(outer);
547 // Call the window resized handler to get the initial sizes setup. Doing
548 // this in a deferred command causes it to occur after all widgets'
549 // sizes have been computed by the browser.
550 Scheduler.get().scheduleIncremental(new RepeatingCommand() {
553 public boolean execute() {
554 if(!isCloudbarReady()) {
557 onWindowResized(Window.getClientHeight());
562 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
564 public void execute() {
565 LOG("Pithos::initialize() Calling Pithos::fetchAccount()");
566 fetchAccount(new Command() {
569 public void execute() {
570 if(!account.hasHomeContainer()) {
571 createHomeContainer(account, this);
573 else if(!account.hasTrashContainer()) {
574 createTrashContainer(this);
577 for(Folder f : account.getContainers()) {
578 if(f.getName().equals(Const.TRASH_CONTAINER)) {
583 trees.add(folderTreeView);
584 folderTreeViewModel.initialize(account, new Command() {
587 public void execute() {
588 createMySharedTree();
592 HorizontalPanel separator = new HorizontalPanel();
593 separator.addStyleName("pithos-statisticsSeparator");
594 separator.add(new HTML(""));
595 trees.add(separator);
597 groupTreeViewModel = new GroupTreeViewModel(Pithos.this);
598 groupTreeView = new GroupTreeView(groupTreeViewModel);
599 treeViews.add(groupTreeView);
600 trees.add(groupTreeView);
601 folderTreeView.showStatistics(account);
609 public void scheduleResfresh() {
610 Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
613 public boolean execute() {
614 final Folder f = getSelection();
619 HeadRequest<Folder> head = new HeadRequest<Folder>(Folder.class, getApiPath(), f.getOwnerID(), "/" + f.getContainer()) {
622 public void onSuccess(Folder _result) {
623 lastModified = new Date();
624 if(getSelectedTree().equals(folderTreeView)) {
625 updateFolder(f, true, new Command() {
628 public void execute() {
634 else if(getSelectedTree().equals(mysharedTreeView)) {
635 updateSharedFolder(f, true, new Command() {
638 public void execute() {
649 public void onError(Throwable t) {
650 if(t instanceof RestException && ((RestException) t).getHttpStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
653 else if(retries >= MAX_RETRIES) {
654 LOG("Error heading folder. ", t);
656 if(t instanceof RestException) {
657 displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
660 displayError("System error heading folder: " + t.getMessage());
664 LOG("Retry ", retries);
665 Scheduler.get().scheduleDeferred(this);
670 protected void onUnauthorized(Response response) {
671 if(retries >= MAX_RETRIES) {
676 Scheduler.get().scheduleDeferred(this);
680 head.setHeader(Const.X_AUTH_TOKEN, getUserToken());
681 head.setHeader(Const.IF_MODIFIED_SINCE, DateTimeFormat.getFormat(Const.DATE_FORMAT_1).format(lastModified, TimeZone.createTimeZone(0)) + " GMT");
682 Scheduler.get().scheduleDeferred(head);
689 public void applyPermissions(Folder f) {
692 upload.setEnabled(false);
696 Boolean[] perms = f.getPermissions().get(userID);
697 if(f.getOwnerID().equals(userID) || (perms != null && perms[1] != null && perms[1])) {
698 upload.setEnabled(true);
702 upload.setEnabled(false);
708 upload.setEnabled(false);
713 @SuppressWarnings({"rawtypes", "unchecked"})
714 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
715 selectedTree = _selectedTree;
717 for(SingleSelectionModel s : selectionModels) {
718 if(!s.equals(model) && s.getSelectedObject() != null) {
719 s.setSelected(s.getSelectedObject(), false);
724 public void showFiles(final Folder f) {
725 Set<File> files = f.getFiles();
729 public void showFiles(Set<File> files) {
730 fileList.setFiles(new ArrayList<File>(files));
734 * Parse and store the user credentials to the appropriate fields.
736 private boolean parseUserCredentials() {
737 Configuration conf = (Configuration) GWT.create(Configuration.class);
738 Dictionary otherProperties = Dictionary.getDictionary(Const.OTHER_PROPERTIES);
739 String cookie = otherProperties.get(Const.AUTH_COOKIE);
740 String auth = Cookies.getCookie(cookie);
745 if(auth.startsWith("\"")) {
746 auth = auth.substring(1);
748 if(auth.endsWith("\"")) {
749 auth = auth.substring(0, auth.length() - 1);
751 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
752 if(authSplit.length != 2) {
756 userID = authSplit[0];
757 userToken = authSplit[1];
759 String gotoUrl = Window.Location.getParameter("goto");
760 if(gotoUrl != null && gotoUrl.length() > 0) {
761 Window.Location.assign(gotoUrl);
768 * Redirect the user to the login page for authentication.
770 protected void authenticateUser() {
771 Dictionary otherProperties = Dictionary.getDictionary(Const.OTHER_PROPERTIES);
772 Window.Location.assign(otherProperties.get(Const.LOGIN_URL) + Window.Location.getHref());
775 public void fetchAccount(final Command callback) {
776 String path = "?format=json";
778 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), userID, path) {
780 public void onSuccess(AccountResource accountResource) {
781 account = accountResource;
782 if(callback != null) {
786 final List<String> memberIDs = new ArrayList<String>();
787 final List<Group> groups = account.getGroups();
788 for(Group group : groups) {
789 memberIDs.addAll(group.getMemberIDs());
791 memberIDs.add(Pithos.this.getUserID());
793 final List<String> theUnknown = Pithos.this.filterUserIDsWithUnknownDisplayName(memberIDs);
794 // Initialize the user catalog
795 new UpdateUserCatalogs(Pithos.this, theUnknown).scheduleDeferred();
796 LOG("Called new UpdateUserCatalogs(Pithos.this, theUnknown).scheduleDeferred();");
800 public void onError(Throwable t) {
801 LOG("Error getting account", t);
803 if(t instanceof RestException) {
804 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
807 displayError("System error fetching user data: " + t.getMessage());
812 protected void onUnauthorized(Response response) {
816 getAccount.setHeader(Const.X_AUTH_TOKEN, userToken);
817 Scheduler.get().scheduleDeferred(getAccount);
820 public void updateStatistics() {
821 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), userID, "", account) {
824 public void onSuccess(AccountResource _result) {
825 folderTreeView.showStatistics(account);
829 public void onError(Throwable t) {
830 LOG("Error getting account", t);
832 if(t instanceof RestException) {
833 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
836 displayError("System error fetching user data: " + t.getMessage());
841 protected void onUnauthorized(Response response) {
845 headAccount.setHeader(Const.X_AUTH_TOKEN, userToken);
846 Scheduler.get().scheduleDeferred(headAccount);
849 protected void createHomeContainer(final AccountResource _account, final Command callback) {
850 String path = "/" + Const.HOME_CONTAINER;
851 PutRequest createPithos = new PutRequest(getApiPath(), getUserID(), path) {
853 public void onSuccess(Resource result) {
854 if(!_account.hasTrashContainer()) {
855 createTrashContainer(callback);
858 fetchAccount(callback);
863 public void onError(Throwable t) {
864 LOG("Error creating pithos", t);
866 if(t instanceof RestException) {
867 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
870 displayError("System error Error creating pithos: " + t.getMessage());
875 protected void onUnauthorized(Response response) {
879 createPithos.setHeader(Const.X_AUTH_TOKEN, getUserToken());
880 Scheduler.get().scheduleDeferred(createPithos);
883 protected void createTrashContainer(final Command callback) {
884 String path = "/" + Const.TRASH_CONTAINER;
885 PutRequest createPithos = new PutRequest(getApiPath(), getUserID(), path) {
887 public void onSuccess(Resource result) {
888 fetchAccount(callback);
892 public void onError(Throwable t) {
893 LOG("Error creating pithos", t);
895 if(t instanceof RestException) {
896 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
899 displayError("System error Error creating pithos: " + t.getMessage());
904 protected void onUnauthorized(Response response) {
908 createPithos.setHeader(Const.X_AUTH_TOKEN, getUserToken());
909 Scheduler.get().scheduleDeferred(createPithos);
913 * Creates an HTML fragment that places an image & caption together, for use
916 * @param imageProto an image prototype for an image
917 * @param caption the group caption
918 * @return the header HTML fragment
920 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
921 String captionHTML = "<table class='caption' cellpadding='0' "
922 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
923 + "</td><td id =" + caption + " class='rcaption'><b style='white-space:nowrap'> "
924 + caption + "</b></td></tr></table>";
928 protected void onWindowResized(int height) {
929 // Adjust the split panel to take up the available room in the window.
930 int newHeight = height - splitPanel.getAbsoluteTop() - 153;
934 splitPanel.setHeight("" + newHeight);
935 inner.setHeight("" + newHeight);
938 native boolean isCloudbarReady()/*-{
939 if ($wnd.$("div.cloudbar") && $wnd.$("div.cloudbar").height() > 0)
945 public void onResize(ResizeEvent event) {
946 int height = event.getHeight();
947 onWindowResized(height);
951 * Display an error message.
953 * @param msg the message to display
955 public void displayError(String msg) {
956 messagePanel.displayError(msg);
957 onWindowResized(Window.getClientHeight());
961 * Display a warning message.
963 * @param msg the message to display
965 public void displayWarning(String msg) {
966 messagePanel.displayWarning(msg);
967 onWindowResized(Window.getClientHeight());
971 * Display an informational message.
973 * @param msg the message to display
975 public void displayInformation(String msg) {
976 messagePanel.displayInformation(msg);
977 onWindowResized(Window.getClientHeight());
981 * Retrieve the fileList.
983 * @return the fileList
985 public FileList getFileList() {
990 * Retrieve the topPanel.
992 * @return the topPanel
994 TopPanel getTopPanel() {
999 * Retrieve the clipboard.
1001 * @return the clipboard
1003 public Clipboard getClipboard() {
1007 public StatusPanel getStatusPanel() {
1011 public String getUserToken() {
1015 public static native void preventIESelection() /*-{
1016 $doc.body.onselectstart = function () {
1021 public static native void enableIESelection() /*-{
1022 if ($doc.body.onselectstart != null)
1023 $doc.body.onselectstart = null;
1027 * @return the absolute path of the API root URL
1029 public String getApiPath() {
1030 Configuration conf = (Configuration) GWT.create(Configuration.class);
1031 return conf.apiPath();
1035 * History support for folder navigation
1036 * adds a new browser history entry
1040 public void updateHistory(String key) {
1041 // Replace any whitespace of the initial string to "+"
1042 // String result = key.replaceAll("\\s","+");
1043 // Add a new browser history entry.
1044 // History.newItem(result);
1045 History.newItem(key);
1048 public void deleteFolder(final Folder folder, final Command callback) {
1049 final PleaseWaitPopup pwp = new PleaseWaitPopup();
1051 String path = "/" + folder.getContainer() + "/" + folder.getPrefix() + "?delimiter=/" + "&t=" + System.currentTimeMillis();
1052 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), folder.getOwnerID(), path) {
1055 protected void onUnauthorized(Response response) {
1061 public void onSuccess(Resource result) {
1062 updateFolder(folder.getParent(), true, new Command() {
1065 public void execute() {
1066 folderTreeSelectionModel.setSelected(folder.getParent(), true);
1068 if(callback != null) {
1077 public void onError(Throwable t) {
1080 if(t instanceof RestException) {
1081 if(((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND) {
1082 displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText());
1089 displayError("System error unable to delete folder: " + t.getMessage());
1094 deleteFolder.setHeader(Const.X_AUTH_TOKEN, getUserToken());
1095 Scheduler.get().scheduleDeferred(deleteFolder);
1098 public FolderTreeView getFolderTreeView() {
1099 return folderTreeView;
1102 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
1103 if(iter.hasNext()) {
1104 File file = iter.next();
1105 String path = targetUri + "/" + file.getName();
1106 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
1108 public void onSuccess(Resource result) {
1109 copyFiles(iter, targetUsername, targetUri, callback);
1113 public void onError(Throwable t) {
1116 if(t instanceof RestException) {
1117 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
1120 displayError("System error unable to copy file: " + t.getMessage());
1125 protected void onUnauthorized(Response response) {
1129 copyFile.setHeader(Const.X_AUTH_TOKEN, getUserToken());
1130 copyFile.setHeader(Const.X_COPY_FROM, URL.encodePathSegment(file.getUri()));
1131 if(!file.getOwnerID().equals(targetUsername)) {
1132 copyFile.setHeader(Const.X_SOURCE_ACCOUNT, URL.encodePathSegment(file.getOwnerID()));
1134 copyFile.setHeader(Const.CONTENT_TYPE, file.getContentType());
1135 Scheduler.get().scheduleDeferred(copyFile);
1137 else if(callback != null) {
1142 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, boolean move, final Command callback) {
1143 String path = targetUri + "?delimiter=/";
1144 PutRequest copyFolder = new PutRequest(getApiPath(), targetUsername, path) {
1146 public void onSuccess(Resource result) {
1147 if(callback != null) {
1153 public void onError(Throwable t) {
1156 if(t instanceof RestException) {
1157 displayError("Unable to copy folder: " + ((RestException) t).getHttpStatusText());
1160 displayError("System error copying folder: " + t.getMessage());
1165 protected void onUnauthorized(Response response) {
1169 copyFolder.setHeader(Const.X_AUTH_TOKEN, getUserToken());
1170 copyFolder.setHeader(Const.ACCEPT, "*/*");
1171 copyFolder.setHeader(Const.CONTENT_LENGTH, "0");
1172 copyFolder.setHeader(Const.CONTENT_TYPE, "application/directory");
1173 if(!f.getOwnerID().equals(targetUsername)) {
1174 copyFolder.setHeader(Const.X_SOURCE_ACCOUNT, f.getOwnerID());
1177 copyFolder.setHeader(Const.X_MOVE_FROM, URL.encodePathSegment(f.getUri()));
1180 copyFolder.setHeader(Const.X_COPY_FROM, URL.encodePathSegment(f.getUri()));
1182 Scheduler.get().scheduleDeferred(copyFolder);
1185 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1186 selectionModels.add(model);
1189 public OtherSharedTreeView getOtherSharedTreeView() {
1190 return otherSharedTreeView;
1193 public void updateTrash(boolean showFiles, Command callback) {
1194 updateFolder(trash, showFiles, callback, true);
1197 public void updateGroupsNode() {
1198 groupTreeView.updateGroupNode(null);
1201 public Group addGroup(String groupname) {
1202 Group newGroup = new Group(groupname);
1203 account.addGroup(newGroup);
1204 groupTreeView.updateGroupNode(null);
1208 public void removeGroup(Group group) {
1209 account.removeGroup(group);
1213 public TreeView getSelectedTree() {
1214 return selectedTree;
1217 public void setSelectedTree(TreeView selected) {
1218 selectedTree = selected;
1221 public Folder getSelection() {
1222 if(selectedTree != null) {
1223 return selectedTree.getSelection();
1228 public void showFolderStatistics(int folderFileCount) {
1229 numOfFiles.setHTML(String.valueOf(folderFileCount));
1232 public GroupTreeView getGroupTreeView() {
1233 return groupTreeView;
1236 public void sessionExpired() {
1237 new SessionExpiredDialog(this).center();
1240 public void updateRootFolder(Command callback) {
1241 updateFolder(account.getPithos(), false, callback, true);
1244 void createMySharedTree() {
1245 LOG("Pithos::createMySharedTree()");
1246 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1247 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1249 public void onSelectionChange(SelectionChangeEvent event) {
1250 if(mysharedTreeSelectionModel.getSelectedObject() != null) {
1251 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1252 upload.setEnabled(false);
1253 disableUploadArea();
1254 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1255 showRelevantToolbarButtons();
1258 if(getSelectedTree().equals(mysharedTreeView)) {
1259 setSelectedTree(null);
1261 if(getSelectedTree() == null) {
1262 showRelevantToolbarButtons();
1267 selectionModels.add(mysharedTreeSelectionModel);
1268 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1269 mysharedTreeViewModel.initialize(new Command() {
1272 public void execute() {
1273 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1274 trees.insert(mysharedTreeView, 2);
1275 treeViews.add(mysharedTreeView);
1276 createOtherSharedTree();
1281 void createOtherSharedTree() {
1282 LOG("Pithos::createOtherSharedTree()");
1283 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1284 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1286 public void onSelectionChange(SelectionChangeEvent event) {
1287 if(otherSharedTreeSelectionModel.getSelectedObject() != null) {
1288 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1289 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1290 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true, null);
1291 showRelevantToolbarButtons();
1294 if(getSelectedTree().equals(otherSharedTreeView)) {
1295 setSelectedTree(null);
1297 if(getSelectedTree() == null) {
1298 showRelevantToolbarButtons();
1303 selectionModels.add(otherSharedTreeSelectionModel);
1304 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1305 LOG("Pithos::createOtherSharedTree(), initializing otherSharedTreeViewModel with a callback");
1306 otherSharedTreeViewModel.initialize(new Command() {
1308 public void execute() {
1309 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1310 trees.insert(otherSharedTreeView, 1);
1311 treeViews.add(otherSharedTreeView);
1317 public String getErrorData() {
1318 final StringBuilder sb = new StringBuilder();
1319 final String NL = Const.NL;
1320 Throwable t = this.error;
1322 sb.append(t.toString());
1324 StackTraceElement[] traces = t.getStackTrace();
1325 for(StackTraceElement trace : traces) {
1327 sb.append(trace.getClassName());
1329 sb.append(trace.getMethodName());
1330 sb.append("() at ");
1331 sb.append(trace.getFileName());
1333 sb.append(trace.getLineNumber());
1340 return sb.toString();
1343 public void setError(Throwable t) {
1348 public void showRelevantToolbarButtons() {
1349 toolbar.showRelevantButtons();
1352 public FileUploadDialog getFileUploadDialog() {
1353 if(fileUploadDialog == null) {
1354 fileUploadDialog = new FileUploadDialog(this);
1356 return fileUploadDialog;
1359 public void hideUploadIndicator() {
1360 upload.removeStyleName("pithos-uploadButton-loading");
1361 upload.setTitle("");
1364 public void showUploadIndicator() {
1365 upload.addStyleName("pithos-uploadButton-loading");
1366 upload.setTitle("Upload in progress. Click for details.");
1369 public void scheduleFolderHeadCommand(final Folder folder, final Command callback) {
1370 if(folder == null) {
1371 if(callback != null) {
1376 HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, getApiPath(), folder.getOwnerID(), folder.getUri(), folder) {
1379 public void onSuccess(Folder _result) {
1380 if(callback != null) {
1386 public void onError(Throwable t) {
1387 if(t instanceof RestException) {
1388 if(((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
1389 final String path = folder.getUri();
1390 PutRequest newFolder = new PutRequest(getApiPath(), folder.getOwnerID(), path) {
1392 public void onSuccess(Resource _result) {
1393 scheduleFolderHeadCommand(folder, callback);
1397 public void onError(Throwable _t) {
1399 if(_t instanceof RestException) {
1400 displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
1403 displayError("System error creating folder: " + _t.getMessage());
1408 protected void onUnauthorized(Response response) {
1412 newFolder.setHeader(Const.X_AUTH_TOKEN, getUserToken());
1413 newFolder.setHeader(Const.CONTENT_TYPE, "application/folder");
1414 newFolder.setHeader(Const.ACCEPT, "*/*");
1415 newFolder.setHeader(Const.CONTENT_LENGTH, "0");
1416 Scheduler.get().scheduleDeferred(newFolder);
1418 else if(((RestException) t).getHttpStatusCode() == Response.SC_FORBIDDEN) {
1422 displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
1426 displayError("System error heading folder: " + t.getMessage());
1429 LOG("Error heading folder", t);
1434 protected void onUnauthorized(Response response) {
1438 headFolder.setHeader(Const.X_AUTH_TOKEN, getUserToken());
1439 Scheduler.get().scheduleDeferred(headFolder);
1443 public void scheduleFileHeadCommand(File f, final Command callback) {
1444 HeadRequest<File> headFile = new HeadRequest<File>(File.class, getApiPath(), f.getOwnerID(), f.getUri(), f) {
1447 public void onSuccess(File _result) {
1448 if(callback != null) {
1454 public void onError(Throwable t) {
1455 LOG("Error heading file", t);
1457 if(t instanceof RestException) {
1458 displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
1461 displayError("System error heading file: " + t.getMessage());
1466 protected void onUnauthorized(Response response) {
1470 headFile.setHeader(Const.X_AUTH_TOKEN, getUserToken());
1471 Scheduler.get().scheduleDeferred(headFile);
1474 public boolean isMySharedSelected() {
1475 return getSelectedTree().equals(getMySharedTreeView());
1478 private Folder getUploadFolder() {
1479 if(folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1480 return getSelection();
1485 private void updateUploadFolder() {
1486 updateUploadFolder(null);
1489 private void updateUploadFolder(final JsArrayString urls) {
1490 if(folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1491 Folder f = getSelection();
1492 if(getSelectedTree().equals(getFolderTreeView())) {
1493 updateFolder(f, true, new Command() {
1496 public void execute() {
1499 selectUploadedFiles(urls);
1505 updateOtherSharedFolder(f, true, null);
1510 public native void disableUploadArea() /*-{
1511 var uploader = $wnd.$("#uploader").pluploadQueue();
1512 var dropElm = $wnd.document.getElementById('rightPanel');
1513 $wnd.plupload.removeAllEvents(dropElm, uploader.id);
1516 public native void enableUploadArea() /*-{
1517 var uploader = $wnd.$("#uploader").pluploadQueue();
1518 var dropElm = $wnd.document.getElementById('rightPanel');
1519 $wnd.plupload.removeAllEvents(dropElm, uploader.id);
1520 if (uploader.runtime == 'html5') {
1521 uploader.settings.drop_element = 'rightPanel';
1522 uploader.trigger('PostInit');
1526 public void showUploadAlert(int nOfFiles) {
1527 if(uploadAlert == null) {
1528 uploadAlert = new UploadAlert(this, nOfFiles);
1530 if(!uploadAlert.isShowing()) {
1531 uploadAlert.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
1534 public void setPosition(int offsetWidth, int offsetHeight) {
1535 uploadAlert.setPopupPosition((Window.getClientWidth() - offsetWidth) / 2, statusPanel.getAbsoluteTop() - offsetHeight);
1539 uploadAlert.setNumOfFiles(nOfFiles);
1542 public void hideUploadAlert() {
1543 if(uploadAlert != null && uploadAlert.isShowing()) {
1548 public void selectUploadedFiles(JsArrayString urls) {
1549 List<String> selectedUrls = new ArrayList<String>();
1550 for(int i = 0; i < urls.length(); i++) {
1551 selectedUrls.add(urls.get(i));
1553 fileList.selectByUrl(selectedUrls);
1556 public void purgeContainer(final Folder container) {
1557 String path = "/" + container.getName() + "?delimiter=/";
1558 DeleteRequest delete = new DeleteRequest(getApiPath(), getUserID(), path) {
1561 protected void onUnauthorized(Response response) {
1566 public void onSuccess(Resource result) {
1567 updateFolder(container, true, null, true);
1571 public void onError(Throwable t) {
1572 LOG("Error deleting trash", t);
1574 if(t instanceof RestException) {
1575 displayError("Error deleting trash: " + ((RestException) t).getHttpStatusText());
1578 displayError("System error deleting trash: " + t.getMessage());
1582 delete.setHeader(Const.X_AUTH_TOKEN, getUserToken());
1583 Scheduler.get().scheduleDeferred(delete);