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 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;
57 import java.util.ArrayList;
58 import java.util.Date;
59 import java.util.HashMap;
60 import java.util.Iterator;
61 import java.util.List;
64 import org.apache.http.HttpStatus;
66 import com.google.gwt.core.client.EntryPoint;
67 import com.google.gwt.core.client.GWT;
68 import com.google.gwt.core.client.JsArrayString;
69 import com.google.gwt.core.client.Scheduler;
70 import com.google.gwt.core.client.Scheduler.RepeatingCommand;
71 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
72 import com.google.gwt.event.dom.client.ClickEvent;
73 import com.google.gwt.event.dom.client.ClickHandler;
74 import com.google.gwt.event.logical.shared.ResizeEvent;
75 import com.google.gwt.event.logical.shared.ResizeHandler;
76 import com.google.gwt.http.client.Response;
77 import com.google.gwt.http.client.URL;
78 import com.google.gwt.i18n.client.DateTimeFormat;
79 import com.google.gwt.i18n.client.Dictionary;
80 import com.google.gwt.i18n.client.TimeZone;
81 import com.google.gwt.resources.client.ClientBundle;
82 import com.google.gwt.resources.client.CssResource;
83 import com.google.gwt.resources.client.ImageResource;
84 import com.google.gwt.resources.client.ImageResource.ImageOptions;
85 import com.google.gwt.user.client.Command;
86 import com.google.gwt.user.client.Cookies;
87 import com.google.gwt.user.client.Event;
88 import com.google.gwt.user.client.History;
89 import com.google.gwt.user.client.Window;
90 import com.google.gwt.user.client.ui.AbstractImagePrototype;
91 import com.google.gwt.user.client.ui.Button;
92 import com.google.gwt.user.client.ui.Composite;
93 import com.google.gwt.user.client.ui.FlowPanel;
94 import com.google.gwt.user.client.ui.HTML;
95 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
96 import com.google.gwt.user.client.ui.HasVerticalAlignment;
97 import com.google.gwt.user.client.ui.HorizontalPanel;
98 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
99 import com.google.gwt.user.client.ui.PopupPanel;
100 import com.google.gwt.user.client.ui.RootPanel;
101 import com.google.gwt.user.client.ui.VerticalPanel;
102 import com.google.gwt.view.client.SelectionChangeEvent;
103 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
104 import com.google.gwt.view.client.SingleSelectionModel;
107 * Entry point classes define <code>onModuleLoad()</code>.
109 public class Pithos implements EntryPoint, ResizeHandler {
111 public static final String HOME_CONTAINER = "pithos";
113 public static final String TRASH_CONTAINER = "trash";
115 public static final Configuration config = GWT.create(Configuration.class);
117 public interface Style extends CssResource {
118 String commandAnchor();
122 @ClassName("gwt-HTML")
125 String uploadAlert();
127 String uploadAlertLink();
129 String uploadAlertProgress();
131 String uploadAlertPercent();
133 String uploadAlertClose();
136 public interface Resources extends ClientBundle {
137 @Source("Pithos.css")
140 @Source("gr/grnet/pithos/resources/close-popup.png")
141 ImageResource closePopup();
144 public static Resources resources = GWT.create(Resources.class);
147 * Instantiate an application-level image bundle. This object will provide
148 * programmatic access to all the images needed by widgets.
150 static Images images = (Images) GWT.create(Images.class);
152 public String getUsername() {
156 public void setAccount(AccountResource acct) {
160 public AccountResource getAccount() {
164 public void updateFolder(Folder f, boolean showfiles, Command callback, final boolean openParent) {
165 folderTreeView.updateFolder(f, showfiles, callback, openParent);
168 public void updateGroupNode(Group group) {
169 groupTreeView.updateGroupNode(group);
172 public void updateMySharedRoot() {
173 mysharedTreeView.updateRoot();
176 public void updateSharedFolder(Folder f, boolean showfiles, Command callback) {
177 mysharedTreeView.updateFolder(f, showfiles, callback);
180 public void updateSharedFolder(Folder f, boolean showfiles) {
181 updateSharedFolder(f, showfiles, null);
184 public void updateOtherSharedFolder(Folder f, boolean showfiles, Command callback) {
185 otherSharedTreeView.updateFolder(f, showfiles, callback);
188 public MysharedTreeView getMySharedTreeView() {
189 return mysharedTreeView;
193 * An aggregate image bundle that pulls together all the images for this
194 * application into a single bundle.
196 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
198 @Source("gr/grnet/pithos/resources/document.png")
199 ImageResource folders();
201 @Source("gr/grnet/pithos/resources/advancedsettings.png")
202 @ImageOptions(width=32, height=32)
203 ImageResource tools();
206 private Throwable error;
209 * The Application Clipboard implementation;
211 private Clipboard clipboard = new Clipboard();
214 * The top panel that contains the menu bar.
216 private TopPanel topPanel;
219 * The panel that contains the various system messages.
221 private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
224 * The bottom panel that contains the status bar.
226 StatusPanel statusPanel = null;
229 * The file list widget.
231 private FileList fileList;
234 * The tab panel that occupies the right side of the screen.
236 private VerticalPanel inner = new VerticalPanel();
240 * The split panel that will contain the left and right panels.
242 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
245 * The currently selected item in the application, for use by the Edit menu
246 * commands. Potential types are Folder, File, User and Group.
248 private Object currentSelection;
250 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
252 private String username = null;
255 * The authentication token of the current user.
257 private String token;
261 SingleSelectionModel<Folder> folderTreeSelectionModel;
262 FolderTreeViewModel folderTreeViewModel;
263 FolderTreeView folderTreeView;
265 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
266 MysharedTreeViewModel mysharedTreeViewModel;
267 MysharedTreeView mysharedTreeView = null;;
269 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
270 OtherSharedTreeViewModel otherSharedTreeViewModel;
271 OtherSharedTreeView otherSharedTreeView = null;
273 GroupTreeViewModel groupTreeViewModel;
274 GroupTreeView groupTreeView;
276 TreeView selectedTree;
277 protected AccountResource account;
281 List<Composite> treeViews = new ArrayList<Composite>();
283 @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
285 public Button upload;
287 private HTML numOfFiles;
289 private Toolbar toolbar;
291 private FileUploadDialog fileUploadDialog = new FileUploadDialog(this);
293 UploadAlert uploadAlert;
298 public void onModuleLoad() {
299 if (parseUserCredentials())
303 private void initialize() {
304 lastModified = new Date(); //Initialize if-modified-since value with now.
305 resources.pithosCss().ensureInjected();
306 boolean bareContent = Window.Location.getParameter("noframe") != null;
307 String contentWidth = bareContent ? "100%" : "75%";
309 VerticalPanel outer = new VerticalPanel();
310 outer.setWidth("100%");
312 outer.addStyleName("pithos-outer");
316 topPanel = new TopPanel(this, Pithos.images);
317 topPanel.setWidth("100%");
319 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
322 messagePanel.setVisible(false);
323 outer.add(messagePanel);
324 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
325 outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
327 HorizontalPanel header = new HorizontalPanel();
328 header.addStyleName("pithos-header");
329 header.setWidth(contentWidth);
331 header.addStyleName("pithos-header-noframe");
332 upload = new Button("Upload", new ClickHandler() {
334 public void onClick(ClickEvent event) {
335 if (getSelection() != null)
336 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
339 upload.addStyleName("pithos-uploadButton");
341 header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
342 header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
344 toolbar = new Toolbar(this);
346 header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
347 header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
349 HorizontalPanel folderStatistics = new HorizontalPanel();
350 folderStatistics.addStyleName("pithos-folderStatistics");
351 numOfFiles = new HTML();
352 folderStatistics.add(numOfFiles);
353 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
354 HTML numOfFilesLabel = new HTML(" Files");
355 folderStatistics.add(numOfFilesLabel);
356 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
357 header.add(folderStatistics);
358 header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
359 header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
360 header.setCellWidth(folderStatistics, "40px");
362 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
363 // Inner contains the various lists
364 inner.sinkEvents(Event.ONCONTEXTMENU);
365 inner.setWidth("100%");
367 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
368 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
370 public void onSelectionChange(SelectionChangeEvent event) {
371 if (folderTreeSelectionModel.getSelectedObject() != null) {
372 deselectOthers(folderTreeView, folderTreeSelectionModel);
373 applyPermissions(folderTreeSelectionModel.getSelectedObject());
374 Folder f = folderTreeSelectionModel.getSelectedObject();
375 updateFolder(f, true, new Command() {
378 public void execute() {
382 showRelevantToolbarButtons();
385 if (getSelectedTree().equals(folderTreeView))
386 setSelectedTree(null);
387 if (getSelectedTree() == null)
388 showRelevantToolbarButtons();
392 selectionModels.add(folderTreeSelectionModel);
394 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
395 folderTreeView = new FolderTreeView(folderTreeViewModel);
396 treeViews.add(folderTreeView);
398 fileList = new FileList(this, images);
401 trees = new VerticalPanel();
402 trees.setWidth("100%");
404 // Add the left and right panels to the split panel.
405 splitPanel.setLeftWidget(trees);
406 FlowPanel right = new FlowPanel();
407 right.getElement().setId("rightPanel");
409 splitPanel.setRightWidget(right);
410 splitPanel.setSplitPosition("219px");
411 splitPanel.setSize("100%", "100%");
412 splitPanel.addStyleName("pithos-splitPanel");
413 splitPanel.setWidth(contentWidth);
414 outer.add(splitPanel);
415 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
418 statusPanel = new StatusPanel();
419 statusPanel.setWidth("100%");
420 outer.add(statusPanel);
421 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
424 splitPanel.addStyleName("pithos-splitPanel-noframe");
426 // Hook the window resize event, so that we can adjust the UI.
427 Window.addResizeHandler(this);
428 // Clear out the window's built-in margin, because we want to take
429 // advantage of the entire client area.
430 Window.setMargin("0px");
431 // Finally, add the outer panel to the RootPanel, so that it will be
433 RootPanel.get().add(outer);
434 // Call the window resized handler to get the initial sizes setup. Doing
435 // this in a deferred command causes it to occur after all widgets'
436 // sizes have been computed by the browser.
437 Scheduler.get().scheduleIncremental(new RepeatingCommand() {
440 public boolean execute() {
441 if (!isCloudbarReady())
443 onWindowResized(Window.getClientHeight());
448 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
450 public void execute() {
451 fetchAccount(new Command() {
454 public void execute() {
455 if (!account.hasHomeContainer())
456 createHomeContainer(account, this);
457 else if (!account.hasTrashContainer())
458 createTrashContainer(this);
460 for (Folder f : account.getContainers())
461 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
465 trees.add(folderTreeView);
466 folderTreeViewModel.initialize(account, new Command() {
469 public void execute() {
470 createMySharedTree();
474 HorizontalPanel separator = new HorizontalPanel();
475 separator.addStyleName("pithos-statisticsSeparator");
476 separator.add(new HTML(""));
477 trees.add(separator);
479 groupTreeViewModel = new GroupTreeViewModel(Pithos.this);
480 groupTreeView = new GroupTreeView(groupTreeViewModel);
481 treeViews.add(groupTreeView);
482 trees.add(groupTreeView);
483 folderTreeView.showStatistics(account);
491 public void scheduleResfresh() {
492 Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
495 public boolean execute() {
496 final Folder f = getSelection();
500 HeadRequest<Folder> head = new HeadRequest<Folder>(Folder.class, getApiPath(), getUsername(), "/" + f.getContainer()) {
503 public void onSuccess(Folder _result) {
504 lastModified = new Date();
505 if (getSelectedTree().equals(folderTreeView))
506 updateFolder(f, true, new Command() {
509 public void execute() {
514 else if (getSelectedTree().equals(mysharedTreeView))
515 updateSharedFolder(f, true, new Command() {
518 public void execute() {
525 public void onError(Throwable t) {
526 if (t instanceof RestException && ((RestException) t).getHttpStatusCode() == HttpStatus.SC_NOT_MODIFIED)
528 else if (retries >= MAX_RETRIES) {
529 GWT.log("Error heading folder", t);
531 if (t instanceof RestException)
532 displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
534 displayError("System error heading folder: " + t.getMessage());
537 GWT.log("Retry " + retries);
538 Scheduler.get().scheduleDeferred(this);
543 protected void onUnauthorized(Response response) {
544 if (retries >= MAX_RETRIES)
547 Scheduler.get().scheduleDeferred(this);
550 head.setHeader("X-Auth-Token", getToken());
551 head.setHeader("If-Modified-Since", DateTimeFormat.getFormat("EEE, dd MMM yyyy HH:mm:ss").format(lastModified, TimeZone.createTimeZone(0)) + " GMT");
552 Scheduler.get().scheduleDeferred(head);
559 public void applyPermissions(Folder f) {
562 upload.setEnabled(false);
566 Boolean[] perms = f.getPermissions().get(username);
567 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
568 upload.setEnabled(true);
572 upload.setEnabled(false);
578 upload.setEnabled(false);
583 @SuppressWarnings({ "rawtypes", "unchecked" })
584 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
585 selectedTree = _selectedTree;
587 for (SingleSelectionModel s : selectionModels)
588 if (!s.equals(model) && s.getSelectedObject() != null)
589 s.setSelected(s.getSelectedObject(), false);
592 public void showFiles(final Folder f) {
593 Set<File> files = f.getFiles();
597 public void showFiles(Set<File> files) {
598 fileList.setFiles(new ArrayList<File>(files));
602 * Parse and store the user credentials to the appropriate fields.
604 private boolean parseUserCredentials() {
605 Configuration conf = (Configuration) GWT.create(Configuration.class);
606 Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
607 String cookie = otherProperties.get("authCookie");
608 String auth = Cookies.getCookie(cookie);
613 if (auth.startsWith("\""))
614 auth = auth.substring(1);
615 if (auth.endsWith("\""))
616 auth = auth.substring(0, auth.length() - 1);
617 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
618 if (authSplit.length != 2) {
622 username = authSplit[0];
623 token = authSplit[1];
625 String gotoUrl = Window.Location.getParameter("goto");
626 if (gotoUrl != null && gotoUrl.length() > 0) {
627 Window.Location.assign(gotoUrl);
634 * Redirect the user to the login page for authentication.
636 protected void authenticateUser() {
637 Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
638 Window.Location.assign(otherProperties.get("loginUrl") + Window.Location.getHref());
641 public void fetchAccount(final Command callback) {
642 String path = "?format=json";
644 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
646 public void onSuccess(AccountResource _result) {
648 if (callback != null)
653 public void onError(Throwable t) {
654 GWT.log("Error getting account", t);
656 if (t instanceof RestException)
657 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
659 displayError("System error fetching user data: " + t.getMessage());
663 protected void onUnauthorized(Response response) {
667 getAccount.setHeader("X-Auth-Token", token);
668 Scheduler.get().scheduleDeferred(getAccount);
671 public void updateStatistics() {
672 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
675 public void onSuccess(AccountResource _result) {
676 folderTreeView.showStatistics(account);
680 public void onError(Throwable t) {
681 GWT.log("Error getting account", t);
683 if (t instanceof RestException)
684 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
686 displayError("System error fetching user data: " + t.getMessage());
690 protected void onUnauthorized(Response response) {
694 headAccount.setHeader("X-Auth-Token", token);
695 Scheduler.get().scheduleDeferred(headAccount);
698 protected void createHomeContainer(final AccountResource _account, final Command callback) {
699 String path = "/" + Pithos.HOME_CONTAINER;
700 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
702 public void onSuccess(Resource result) {
703 if (!_account.hasTrashContainer())
704 createTrashContainer(callback);
706 fetchAccount(callback);
710 public void onError(Throwable t) {
711 GWT.log("Error creating pithos", t);
713 if (t instanceof RestException)
714 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
716 displayError("System error Error creating pithos: " + t.getMessage());
720 protected void onUnauthorized(Response response) {
724 createPithos.setHeader("X-Auth-Token", getToken());
725 Scheduler.get().scheduleDeferred(createPithos);
728 protected void createTrashContainer(final Command callback) {
729 String path = "/" + Pithos.TRASH_CONTAINER;
730 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
732 public void onSuccess(Resource result) {
733 fetchAccount(callback);
737 public void onError(Throwable t) {
738 GWT.log("Error creating pithos", t);
740 if (t instanceof RestException)
741 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
743 displayError("System error Error creating pithos: " + t.getMessage());
747 protected void onUnauthorized(Response response) {
751 createPithos.setHeader("X-Auth-Token", getToken());
752 Scheduler.get().scheduleDeferred(createPithos);
756 * Creates an HTML fragment that places an image & caption together, for use
759 * @param imageProto an image prototype for an image
760 * @param caption the group caption
761 * @return the header HTML fragment
763 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
764 String captionHTML = "<table class='caption' cellpadding='0' "
765 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
766 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
767 + caption + "</b></td></tr></table>";
771 protected void onWindowResized(int height) {
772 // Adjust the split panel to take up the available room in the window.
773 int newHeight = height - splitPanel.getAbsoluteTop() - 153;
776 splitPanel.setHeight("" + newHeight);
777 inner.setHeight("" + newHeight);
780 native boolean isCloudbarReady()/*-{
781 if ($wnd.$("div.cloudbar") && $wnd.$("div.cloudbar").height() > 0)
787 public void onResize(ResizeEvent event) {
788 int height = event.getHeight();
789 onWindowResized(height);
793 * Display an error message.
795 * @param msg the message to display
797 public void displayError(String msg) {
798 messagePanel.displayError(msg);
799 onWindowResized(Window.getClientHeight());
803 * Display a warning message.
805 * @param msg the message to display
807 public void displayWarning(String msg) {
808 messagePanel.displayWarning(msg);
809 onWindowResized(Window.getClientHeight());
813 * Display an informational message.
815 * @param msg the message to display
817 public void displayInformation(String msg) {
818 messagePanel.displayInformation(msg);
819 onWindowResized(Window.getClientHeight());
823 * Retrieve the fileList.
825 * @return the fileList
827 public FileList getFileList() {
832 * Retrieve the topPanel.
834 * @return the topPanel
836 TopPanel getTopPanel() {
841 * Retrieve the clipboard.
843 * @return the clipboard
845 public Clipboard getClipboard() {
849 public StatusPanel getStatusPanel() {
853 public String getToken() {
857 public static native void preventIESelection() /*-{
858 $doc.body.onselectstart = function() {
863 public static native void enableIESelection() /*-{
864 if ($doc.body.onselectstart != null)
865 $doc.body.onselectstart = null;
869 * @return the absolute path of the API root URL
871 public String getApiPath() {
872 Configuration conf = (Configuration) GWT.create(Configuration.class);
873 return conf.apiPath();
877 * History support for folder navigation
878 * adds a new browser history entry
882 public void updateHistory(String key){
883 // Replace any whitespace of the initial string to "+"
884 // String result = key.replaceAll("\\s","+");
885 // Add a new browser history entry.
886 // History.newItem(result);
887 History.newItem(key);
890 public void deleteFolder(final Folder folder, final Command callback) {
891 final PleaseWaitPopup pwp = new PleaseWaitPopup();
893 String path = "/" + folder.getContainer() + "/" + folder.getPrefix() + "?delimiter=/" + "&t=" + System.currentTimeMillis();
894 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), folder.getOwner(), path) {
897 protected void onUnauthorized(Response response) {
903 public void onSuccess(Resource result) {
904 updateFolder(folder.getParent(), true, new Command() {
907 public void execute() {
908 folderTreeSelectionModel.setSelected(folder.getParent(), true);
910 if (callback != null)
918 public void onError(Throwable t) {
921 if (t instanceof RestException) {
922 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
923 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
928 displayError("System error unable to delete folder: " + t.getMessage());
932 deleteFolder.setHeader("X-Auth-Token", getToken());
933 Scheduler.get().scheduleDeferred(deleteFolder);
936 public FolderTreeView getFolderTreeView() {
937 return folderTreeView;
940 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
941 if (iter.hasNext()) {
942 File file = iter.next();
943 String path = targetUri + "/" + file.getName();
944 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
946 public void onSuccess(Resource result) {
947 copyFiles(iter, targetUsername, targetUri, callback);
951 public void onError(Throwable t) {
954 if (t instanceof RestException) {
955 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
958 displayError("System error unable to copy file: "+t.getMessage());
962 protected void onUnauthorized(Response response) {
966 copyFile.setHeader("X-Auth-Token", getToken());
967 copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
968 if (!file.getOwner().equals(targetUsername))
969 copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwner()));
970 copyFile.setHeader("Content-Type", file.getContentType());
971 Scheduler.get().scheduleDeferred(copyFile);
973 else if (callback != null) {
978 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, boolean move, final Command callback) {
979 String path = targetUri + "?delimiter=/";
980 PutRequest copyFolder = new PutRequest(getApiPath(), targetUsername, path) {
982 public void onSuccess(Resource result) {
983 if (callback != null)
988 public void onError(Throwable t) {
991 if (t instanceof RestException) {
992 displayError("Unable to copy folder: " + ((RestException) t).getHttpStatusText());
995 displayError("System error copying folder: " + t.getMessage());
999 protected void onUnauthorized(Response response) {
1003 copyFolder.setHeader("X-Auth-Token", getToken());
1004 copyFolder.setHeader("Accept", "*/*");
1005 copyFolder.setHeader("Content-Length", "0");
1006 copyFolder.setHeader("Content-Type", "application/directory");
1007 if (!f.getOwner().equals(targetUsername))
1008 copyFolder.setHeader("X-Source-Account", f.getOwner());
1010 copyFolder.setHeader("X-Move-From", URL.encodePathSegment(f.getUri()));
1012 copyFolder.setHeader("X-Copy-From", URL.encodePathSegment(f.getUri()));
1013 Scheduler.get().scheduleDeferred(copyFolder);
1016 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1017 selectionModels.add(model);
1020 public OtherSharedTreeView getOtherSharedTreeView() {
1021 return otherSharedTreeView;
1024 public void updateTrash(boolean showFiles, Command callback) {
1025 updateFolder(trash, showFiles, callback, true);
1028 public void updateGroupsNode() {
1029 groupTreeView.updateGroupNode(null);
1032 public Group addGroup(String groupname) {
1033 Group newGroup = new Group(groupname);
1034 account.addGroup(newGroup);
1035 groupTreeView.updateGroupNode(null);
1039 public void removeGroup(Group group) {
1040 account.removeGroup(group);
1044 public TreeView getSelectedTree() {
1045 return selectedTree;
1048 public void setSelectedTree(TreeView selected) {
1049 selectedTree = selected;
1052 public Folder getSelection() {
1053 if (selectedTree != null)
1054 return selectedTree.getSelection();
1058 public void showFolderStatistics(int folderFileCount) {
1059 numOfFiles.setHTML(String.valueOf(folderFileCount));
1062 public GroupTreeView getGroupTreeView() {
1063 return groupTreeView;
1066 public void sessionExpired() {
1067 new SessionExpiredDialog(this).center();
1070 public void updateRootFolder(Command callback) {
1071 updateFolder(account.getPithos(), false, callback, true);
1074 void createMySharedTree() {
1075 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1076 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1078 public void onSelectionChange(SelectionChangeEvent event) {
1079 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1080 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1081 upload.setEnabled(false);
1082 disableUploadArea();
1083 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1084 showRelevantToolbarButtons();
1087 if (getSelectedTree().equals(mysharedTreeView))
1088 setSelectedTree(null);
1089 if (getSelectedTree() == null)
1090 showRelevantToolbarButtons();
1094 selectionModels.add(mysharedTreeSelectionModel);
1095 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1096 mysharedTreeViewModel.initialize(new Command() {
1099 public void execute() {
1100 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1101 trees.insert(mysharedTreeView, 2);
1102 treeViews.add(mysharedTreeView);
1103 createOtherSharedTree();
1108 void createOtherSharedTree() {
1109 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1110 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1112 public void onSelectionChange(SelectionChangeEvent event) {
1113 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1114 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1115 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1116 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true, null);
1117 showRelevantToolbarButtons();
1120 if (getSelectedTree().equals(otherSharedTreeView))
1121 setSelectedTree(null);
1122 if (getSelectedTree() == null)
1123 showRelevantToolbarButtons();
1127 selectionModels.add(otherSharedTreeSelectionModel);
1128 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1129 otherSharedTreeViewModel.initialize(new Command() {
1132 public void execute() {
1133 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1134 trees.insert(otherSharedTreeView, 1);
1135 treeViews.add(otherSharedTreeView);
1141 public native void log1(String message)/*-{
1142 $wnd.console.log(message);
1145 public String getErrorData() {
1147 return error.toString();
1151 public void setError(Throwable t) {
1155 public void showRelevantToolbarButtons() {
1156 toolbar.showRelevantButtons();
1159 public FileUploadDialog getFileUploadDialog() {
1160 if (fileUploadDialog == null)
1161 fileUploadDialog = new FileUploadDialog(this);
1162 return fileUploadDialog;
1165 public void hideUploadIndicator() {
1166 upload.removeStyleName("pithos-uploadButton-loading");
1167 upload.setTitle("");
1170 public void showUploadIndicator() {
1171 upload.addStyleName("pithos-uploadButton-loading");
1172 upload.setTitle("Upload in progress. Click for details.");
1175 public void scheduleFolderHeadCommand(final Folder folder, final Command callback) {
1176 if (folder == null) {
1177 if (callback != null)
1181 HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, getApiPath(), folder.getOwner(), folder.getUri(), folder) {
1184 public void onSuccess(Folder _result) {
1185 if (callback != null)
1190 public void onError(Throwable t) {
1191 if (t instanceof RestException) {
1192 if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
1193 final String path = folder.getUri();
1194 PutRequest newFolder = new PutRequest(getApiPath(), folder.getOwner(), path) {
1196 public void onSuccess(Resource _result) {
1197 scheduleFolderHeadCommand(folder, callback);
1201 public void onError(Throwable _t) {
1204 if(_t instanceof RestException){
1205 displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
1208 displayError("System error creating folder: " + _t.getMessage());
1212 protected void onUnauthorized(Response response) {
1216 newFolder.setHeader("X-Auth-Token", getToken());
1217 newFolder.setHeader("Content-Type", "application/folder");
1218 newFolder.setHeader("Accept", "*/*");
1219 newFolder.setHeader("Content-Length", "0");
1220 Scheduler.get().scheduleDeferred(newFolder);
1223 displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
1226 displayError("System error heading folder: " + t.getMessage());
1228 GWT.log("Error heading folder", t);
1233 protected void onUnauthorized(Response response) {
1237 headFolder.setHeader("X-Auth-Token", getToken());
1238 Scheduler.get().scheduleDeferred(headFolder);
1242 public void scheduleFileHeadCommand(File f, final Command callback) {
1243 HeadRequest<File> headFile = new HeadRequest<File>(File.class, getApiPath(), f.getOwner(), f.getUri(), f) {
1246 public void onSuccess(File _result) {
1247 if (callback != null)
1252 public void onError(Throwable t) {
1253 GWT.log("Error heading file", t);
1255 if (t instanceof RestException)
1256 displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
1258 displayError("System error heading file: " + t.getMessage());
1262 protected void onUnauthorized(Response response) {
1266 headFile.setHeader("X-Auth-Token", getToken());
1267 Scheduler.get().scheduleDeferred(headFile);
1270 public boolean isMySharedSelected() {
1271 return getSelectedTree().equals(getMySharedTreeView());
1274 private Folder getUploadFolder() {
1275 if (folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1276 return getSelection();
1281 private void updateUploadFolder() {
1282 updateUploadFolder(null);
1285 private void updateUploadFolder(final JsArrayString urls) {
1286 if (folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1287 Folder f = getSelection();
1288 if (getSelectedTree().equals(getFolderTreeView()))
1289 updateFolder(f, true, new Command() {
1292 public void execute() {
1295 selectUploadedFiles(urls);
1299 updateOtherSharedFolder(f, true, null);
1303 public native void disableUploadArea() /*-{
1304 var uploader = $wnd.$("#uploader").pluploadQueue();
1305 var dropElm = $wnd.document.getElementById('rightPanel');
1306 $wnd.plupload.removeAllEvents(dropElm, uploader.id);
1309 public native void enableUploadArea() /*-{
1310 var uploader = $wnd.$("#uploader").pluploadQueue();
1311 var dropElm = $wnd.document.getElementById('rightPanel');
1312 $wnd.plupload.removeAllEvents(dropElm, uploader.id);
1313 if (uploader.runtime == 'html5') {
1314 uploader.settings.drop_element = 'rightPanel';
1315 uploader.trigger('PostInit');
1319 public void showUploadAlert(int nOfFiles) {
1320 if (uploadAlert == null)
1321 uploadAlert = new UploadAlert(this, nOfFiles);
1322 if (!uploadAlert.isShowing())
1323 uploadAlert.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
1326 public void setPosition(int offsetWidth, int offsetHeight) {
1327 uploadAlert.setPopupPosition((Window.getClientWidth() - offsetWidth)/2, statusPanel.getAbsoluteTop() - offsetHeight);
1330 uploadAlert.setNumOfFiles(nOfFiles);
1333 public void hideUploadAlert() {
1334 if (uploadAlert != null && uploadAlert.isShowing())
1338 public void selectUploadedFiles(JsArrayString urls) {
1339 List<String> selectedUrls = new ArrayList<String>();
1340 for (int i=0; i<urls.length(); i++)
1341 selectedUrls.add(urls.get(i));
1342 fileList.selectByUrl(selectedUrls);
1345 public void emptyContainer(final Folder container) {
1346 String path = "/" + container.getName() + "?delimiter=/";
1347 DeleteRequest delete = new DeleteRequest(getApiPath(), getUsername(), path) {
1350 protected void onUnauthorized(Response response) {
1355 public void onSuccess(Resource result) {
1356 updateFolder(container, true, null, true);
1360 public void onError(Throwable t) {
1361 GWT.log("Error deleting trash", t);
1363 if (t instanceof RestException)
1364 displayError("Error deleting trash: " + ((RestException) t).getHttpStatusText());
1366 displayError("System error deleting trash: " + t.getMessage());
1369 delete.setHeader("X-Auth-Token", getToken());
1370 Scheduler.get().scheduleDeferred(delete);