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.HashMap;
59 import java.util.Iterator;
60 import java.util.List;
63 import com.google.gwt.core.client.EntryPoint;
64 import com.google.gwt.core.client.GWT;
65 import com.google.gwt.core.client.Scheduler;
66 import com.google.gwt.core.client.Scheduler.RepeatingCommand;
67 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
68 import com.google.gwt.event.dom.client.ClickEvent;
69 import com.google.gwt.event.dom.client.ClickHandler;
70 import com.google.gwt.event.logical.shared.ResizeEvent;
71 import com.google.gwt.event.logical.shared.ResizeHandler;
72 import com.google.gwt.http.client.Request;
73 import com.google.gwt.http.client.RequestBuilder;
74 import com.google.gwt.http.client.RequestCallback;
75 import com.google.gwt.http.client.RequestException;
76 import com.google.gwt.http.client.Response;
77 import com.google.gwt.http.client.URL;
78 import com.google.gwt.i18n.client.Dictionary;
79 import com.google.gwt.i18n.client.NumberFormat;
80 import com.google.gwt.json.client.JSONArray;
81 import com.google.gwt.json.client.JSONObject;
82 import com.google.gwt.json.client.JSONParser;
83 import com.google.gwt.json.client.JSONString;
84 import com.google.gwt.json.client.JSONValue;
85 import com.google.gwt.resources.client.ImageResource;
86 import com.google.gwt.resources.client.ImageResource.ImageOptions;
87 import com.google.gwt.user.client.Command;
88 import com.google.gwt.user.client.Cookies;
89 import com.google.gwt.user.client.Event;
90 import com.google.gwt.user.client.History;
91 import com.google.gwt.user.client.Window;
92 import com.google.gwt.user.client.ui.AbstractImagePrototype;
93 import com.google.gwt.user.client.ui.Button;
94 import com.google.gwt.user.client.ui.Composite;
95 import com.google.gwt.user.client.ui.FlowPanel;
96 import com.google.gwt.user.client.ui.HTML;
97 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
98 import com.google.gwt.user.client.ui.HasVerticalAlignment;
99 import com.google.gwt.user.client.ui.HorizontalPanel;
100 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
101 import com.google.gwt.user.client.ui.RootPanel;
102 import com.google.gwt.user.client.ui.VerticalPanel;
103 import com.google.gwt.view.client.SelectionChangeEvent;
104 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
105 import com.google.gwt.view.client.SingleSelectionModel;
108 * Entry point classes define <code>onModuleLoad()</code>.
110 public class Pithos implements EntryPoint, ResizeHandler {
112 public static final String HOME_CONTAINER = "pithos";
114 public static final String TRASH_CONTAINER = "trash";
116 public static final Configuration config = GWT.create(Configuration.class);
119 * Instantiate an application-level image bundle. This object will provide
120 * programmatic access to all the images needed by widgets.
122 static Images images = (Images) GWT.create(Images.class);
124 public String getUsername() {
128 public void setAccount(AccountResource acct) {
132 public AccountResource getAccount() {
136 public void updateFolder(Folder f, boolean showfiles, Command callback, final boolean openParent) {
137 folderTreeView.updateFolder(f, showfiles, callback, openParent);
140 public void updateGroupNode(Group group) {
141 groupTreeView.updateGroupNode(group);
144 public void updateMySharedRoot() {
145 mysharedTreeView.updateRoot();
148 public void updateSharedFolder(Folder f, boolean showfiles) {
149 mysharedTreeView.updateFolder(f, showfiles);
152 public void updateOtherSharedFolder(Folder f, boolean showfiles) {
153 otherSharedTreeView.updateFolder(f, showfiles);
156 public MysharedTreeView getMySharedTreeView() {
157 return mysharedTreeView;
161 * An aggregate image bundle that pulls together all the images for this
162 * application into a single bundle.
164 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
166 @Source("gr/grnet/pithos/resources/document.png")
167 ImageResource folders();
169 @Source("gr/grnet/pithos/resources/advancedsettings.png")
170 @ImageOptions(width=32, height=32)
171 ImageResource tools();
174 private Throwable error;
177 * The Application Clipboard implementation;
179 private Clipboard clipboard = new Clipboard();
182 * The top panel that contains the menu bar.
184 private TopPanel topPanel;
187 * The panel that contains the various system messages.
189 private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
192 * The bottom panel that contains the status bar.
194 private StatusPanel statusPanel = null;
197 * The file list widget.
199 private FileList fileList;
202 * The tab panel that occupies the right side of the screen.
204 private VerticalPanel inner = new VerticalPanel();
208 * The split panel that will contain the left and right panels.
210 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
213 * The currently selected item in the application, for use by the Edit menu
214 * commands. Potential types are Folder, File, User and Group.
216 private Object currentSelection;
218 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
220 private String username = null;
223 * The authentication token of the current user.
225 private String token;
229 SingleSelectionModel<Folder> folderTreeSelectionModel;
230 FolderTreeViewModel folderTreeViewModel;
231 FolderTreeView folderTreeView;
233 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
234 MysharedTreeViewModel mysharedTreeViewModel;
235 MysharedTreeView mysharedTreeView = null;;
237 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
238 OtherSharedTreeViewModel otherSharedTreeViewModel;
239 OtherSharedTreeView otherSharedTreeView = null;
241 GroupTreeViewModel groupTreeViewModel;
242 GroupTreeView groupTreeView;
244 TreeView selectedTree;
245 protected AccountResource account;
249 List<Composite> treeViews = new ArrayList<Composite>();
251 @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
253 public Button upload;
255 private HTML numOfFiles;
257 private Toolbar toolbar;
259 private FileUploadDialog fileUploadDialog;
262 public void onModuleLoad() {
263 if (parseUserCredentials())
267 private void initialize() {
268 boolean bareContent = Window.Location.getParameter("noframe") != null;
269 String contentWidth = bareContent ? "100%" : "75%";
271 VerticalPanel outer = new VerticalPanel();
272 outer.setWidth("100%");
274 outer.addStyleName("pithos-outer");
278 topPanel = new TopPanel(this, Pithos.images);
279 topPanel.setWidth("100%");
281 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
284 messagePanel.setVisible(false);
285 outer.add(messagePanel);
286 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
287 outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
289 HorizontalPanel header = new HorizontalPanel();
290 header.addStyleName("pithos-header");
291 header.setWidth(contentWidth);
293 header.addStyleName("pithos-header-noframe");
294 upload = new Button("Upload", new ClickHandler() {
296 public void onClick(ClickEvent event) {
297 if (getSelection() != null)
298 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
301 upload.addStyleName("pithos-uploadButton");
303 header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
304 header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
306 toolbar = new Toolbar(this);
308 header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
309 header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
311 HorizontalPanel folderStatistics = new HorizontalPanel();
312 folderStatistics.addStyleName("pithos-folderStatistics");
313 numOfFiles = new HTML();
314 folderStatistics.add(numOfFiles);
315 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
316 HTML numOfFilesLabel = new HTML(" Files");
317 folderStatistics.add(numOfFilesLabel);
318 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
319 header.add(folderStatistics);
320 header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
321 header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
322 header.setCellWidth(folderStatistics, "40px");
324 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
325 // Inner contains the various lists.nner
326 inner.sinkEvents(Event.ONCONTEXTMENU);
327 inner.setWidth("100%");
329 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
330 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
332 public void onSelectionChange(SelectionChangeEvent event) {
333 if (folderTreeSelectionModel.getSelectedObject() != null) {
334 deselectOthers(folderTreeView, folderTreeSelectionModel);
335 applyPermissions(folderTreeSelectionModel.getSelectedObject());
336 Folder f = folderTreeSelectionModel.getSelectedObject();
337 updateFolder(f, true, new Command() {
340 public void execute() {
344 showRelevantToolbarButtons();
347 if (getSelectedTree().equals(folderTreeView))
348 setSelectedTree(null);
349 if (getSelectedTree() == null)
350 showRelevantToolbarButtons();
354 selectionModels.add(folderTreeSelectionModel);
356 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
357 folderTreeView = new FolderTreeView(folderTreeViewModel);
358 treeViews.add(folderTreeView);
360 fileList = new FileList(this, images);
363 inner.add(createUploadArea());
365 trees = new VerticalPanel();
366 trees.setWidth("100%");
368 // Add the left and right panels to the split panel.
369 splitPanel.setLeftWidget(trees);
370 splitPanel.setRightWidget(inner);
371 splitPanel.setSplitPosition("219px");
372 splitPanel.setSize("100%", "100%");
373 splitPanel.addStyleName("pithos-splitPanel");
374 splitPanel.setWidth(contentWidth);
375 outer.add(splitPanel);
376 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
379 statusPanel = new StatusPanel();
380 statusPanel.setWidth("100%");
381 outer.add(statusPanel);
382 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
385 splitPanel.addStyleName("pithos-splitPanel-noframe");
387 // Hook the window resize event, so that we can adjust the UI.
388 Window.addResizeHandler(this);
389 // Clear out the window's built-in margin, because we want to take
390 // advantage of the entire client area.
391 Window.setMargin("0px");
392 // Finally, add the outer panel to the RootPanel, so that it will be
394 RootPanel.get().add(outer);
395 // Call the window resized handler to get the initial sizes setup. Doing
396 // this in a deferred command causes it to occur after all widgets'
397 // sizes have been computed by the browser.
398 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
401 public void execute() {
402 onWindowResized(Window.getClientHeight());
406 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
408 public void execute() {
409 fetchAccount(new Command() {
412 public void execute() {
413 if (!account.hasHomeContainer())
414 createHomeContainer(account, this);
415 else if (!account.hasTrashContainer())
416 createTrashContainer(this);
418 for (Folder f : account.getContainers())
419 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
423 trees.add(folderTreeView);
424 folderTreeViewModel.initialize(account, new Command() {
427 public void execute() {
428 createMySharedTree();
432 HorizontalPanel separator = new HorizontalPanel();
433 separator.addStyleName("pithos-statisticsSeparator");
434 separator.add(new HTML(""));
435 trees.add(separator);
437 groupTreeViewModel = new GroupTreeViewModel(Pithos.this);
438 groupTreeView = new GroupTreeView(groupTreeViewModel);
439 treeViews.add(groupTreeView);
440 trees.add(groupTreeView);
441 folderTreeView.showStatistics(account);
448 Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
451 public boolean execute() {
452 Folder f = getSelection();
454 if (getSelectedTree().equals(folderTreeView))
455 updateFolder(f, true, null, false);
456 else if (getSelectedTree().equals(mysharedTreeView))
457 updateSharedFolder(f, true);
464 public void applyPermissions(Folder f) {
467 upload.setEnabled(false);
471 Boolean[] perms = f.getPermissions().get(username);
472 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
473 upload.setEnabled(true);
477 upload.setEnabled(false);
483 upload.setEnabled(false);
488 @SuppressWarnings({ "rawtypes", "unchecked" })
489 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
490 selectedTree = _selectedTree;
492 for (SingleSelectionModel s : selectionModels)
493 if (!s.equals(model) && s.getSelectedObject() != null)
494 s.setSelected(s.getSelectedObject(), false);
497 public void showFiles(final Folder f) {
498 Set<File> files = f.getFiles();
502 public void showFiles(Set<File> files) {
503 fileList.setFiles(new ArrayList<File>(files));
507 * Parse and store the user credentials to the appropriate fields.
509 private boolean parseUserCredentials() {
510 Configuration conf = (Configuration) GWT.create(Configuration.class);
511 Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
512 String cookie = otherProperties.get("authCookie");
513 String auth = Cookies.getCookie(cookie);
518 if (auth.startsWith("\""))
519 auth = auth.substring(1);
520 if (auth.endsWith("\""))
521 auth = auth.substring(0, auth.length() - 1);
522 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
523 if (authSplit.length != 2) {
527 username = authSplit[0];
528 token = authSplit[1];
530 String gotoUrl = Window.Location.getParameter("goto");
531 if (gotoUrl != null && gotoUrl.length() > 0) {
532 Window.Location.assign(gotoUrl);
539 * Redirect the user to the login page for authentication.
541 protected void authenticateUser() {
542 Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
543 Window.Location.assign(otherProperties.get("loginUrl") + Window.Location.getHref());
546 protected void fetchAccount(final Command callback) {
547 String path = "?format=json";
549 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
551 public void onSuccess(AccountResource _result) {
553 if (callback != null)
558 public void onError(Throwable t) {
559 GWT.log("Error getting account", t);
561 if (t instanceof RestException)
562 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
564 displayError("System error fetching user data: " + t.getMessage());
568 protected void onUnauthorized(Response response) {
572 getAccount.setHeader("X-Auth-Token", token);
573 Scheduler.get().scheduleDeferred(getAccount);
576 public void updateStatistics() {
577 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
580 public void onSuccess(AccountResource _result) {
581 folderTreeView.showStatistics(account);
585 public void onError(Throwable t) {
586 GWT.log("Error getting account", t);
588 if (t instanceof RestException)
589 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
591 displayError("System error fetching user data: " + t.getMessage());
595 protected void onUnauthorized(Response response) {
599 headAccount.setHeader("X-Auth-Token", token);
600 Scheduler.get().scheduleDeferred(headAccount);
603 protected void createHomeContainer(final AccountResource _account, final Command callback) {
604 String path = "/" + Pithos.HOME_CONTAINER;
605 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
607 public void onSuccess(Resource result) {
608 if (!_account.hasTrashContainer())
609 createTrashContainer(callback);
611 fetchAccount(callback);
615 public void onError(Throwable t) {
616 GWT.log("Error creating pithos", t);
618 if (t instanceof RestException)
619 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
621 displayError("System error Error creating pithos: " + t.getMessage());
625 protected void onUnauthorized(Response response) {
629 createPithos.setHeader("X-Auth-Token", getToken());
630 Scheduler.get().scheduleDeferred(createPithos);
633 protected void createTrashContainer(final Command callback) {
634 String path = "/" + Pithos.TRASH_CONTAINER;
635 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
637 public void onSuccess(Resource result) {
638 fetchAccount(callback);
642 public void onError(Throwable t) {
643 GWT.log("Error creating pithos", t);
645 if (t instanceof RestException)
646 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
648 displayError("System error Error creating pithos: " + t.getMessage());
652 protected void onUnauthorized(Response response) {
656 createPithos.setHeader("X-Auth-Token", getToken());
657 Scheduler.get().scheduleDeferred(createPithos);
661 * Creates an HTML fragment that places an image & caption together, for use
664 * @param imageProto an image prototype for an image
665 * @param caption the group caption
666 * @return the header HTML fragment
668 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
669 String captionHTML = "<table class='caption' cellpadding='0' "
670 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
671 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
672 + caption + "</b></td></tr></table>";
676 protected void onWindowResized(int height) {
677 // Adjust the split panel to take up the available room in the window.
678 int newHeight = height - splitPanel.getAbsoluteTop();
681 splitPanel.setHeight("" + newHeight);
682 inner.setHeight("" + newHeight);
686 public void onResize(ResizeEvent event) {
687 int height = event.getHeight();
688 onWindowResized(height);
692 * Display an error message.
694 * @param msg the message to display
696 public void displayError(String msg) {
697 messagePanel.displayError(msg);
701 * Display a warning message.
703 * @param msg the message to display
705 public void displayWarning(String msg) {
706 messagePanel.displayWarning(msg);
710 * Display an informational message.
712 * @param msg the message to display
714 public void displayInformation(String msg) {
715 messagePanel.displayInformation(msg);
719 * Retrieve the fileList.
721 * @return the fileList
723 public FileList getFileList() {
728 * Retrieve the topPanel.
730 * @return the topPanel
732 TopPanel getTopPanel() {
737 * Retrieve the clipboard.
739 * @return the clipboard
741 public Clipboard getClipboard() {
745 public StatusPanel getStatusPanel() {
749 public String getToken() {
753 public static native void preventIESelection() /*-{
754 $doc.body.onselectstart = function () { return false; };
757 public static native void enableIESelection() /*-{
758 if ($doc.body.onselectstart != null)
759 $doc.body.onselectstart = null;
763 * @return the absolute path of the API root URL
765 public String getApiPath() {
766 Configuration conf = (Configuration) GWT.create(Configuration.class);
767 return conf.apiPath();
771 * History support for folder navigation
772 * adds a new browser history entry
776 public void updateHistory(String key){
777 // Replace any whitespace of the initial string to "+"
778 // String result = key.replaceAll("\\s","+");
779 // Add a new browser history entry.
780 // History.newItem(result);
781 History.newItem(key);
784 public void deleteFolder(final Folder folder, final Command callback) {
785 String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
786 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
787 builder.setHeader("X-Auth-Token", getToken());
789 builder.sendRequest("", new RequestCallback() {
791 public void onResponseReceived(Request request, Response response) {
792 if (response.getStatusCode() == Response.SC_OK) {
793 JSONValue json = JSONParser.parseStrict(response.getText());
794 JSONArray array = json.isArray();
797 deleteObject(folder, i, array, callback);
803 public void onError(Request request, Throwable exception) {
805 displayError("System error unable to delete folder: " + exception.getMessage());
809 catch (RequestException e) {
813 void deleteObject(final Folder folder, final int i, final JSONArray array, final Command callback) {
814 if (i < array.size()) {
815 JSONObject o = array.get(i).isObject();
816 if (o != null && !o.containsKey("subdir")) {
817 JSONString name = o.get("name").isString();
818 String path = "/" + folder.getContainer() + "/" + name.stringValue();
819 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), URL.encode(path)) {
821 public void onSuccess(Resource result) {
822 deleteObject(folder, i + 1, array, callback);
826 public void onError(Throwable t) {
829 displayError("System error unable to delete folder: " + t.getMessage());
833 protected void onUnauthorized(Response response) {
837 delete.setHeader("X-Auth-Token", getToken());
838 Scheduler.get().scheduleDeferred(delete);
840 else if (o != null) {
841 String subdir = o.get("subdir").isString().stringValue();
842 subdir = subdir.substring(0, subdir.length() - 1);
843 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
844 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
845 builder.setHeader("X-Auth-Token", getToken());
847 builder.sendRequest("", new RequestCallback() {
849 public void onResponseReceived(Request request, Response response) {
850 if (response.getStatusCode() == Response.SC_OK) {
851 JSONValue json = JSONParser.parseStrict(response.getText());
852 JSONArray array2 = json.isArray();
853 if (array2 != null) {
854 int l = array.size();
855 for (int j=0; j<array2.size(); j++) {
856 array.set(l++, array2.get(j));
859 deleteObject(folder, i + 1, array, callback);
864 public void onError(Request request, Throwable exception) {
866 displayError("System error unable to delete folder: " + exception.getMessage());
870 catch (RequestException e) {
875 String path = folder.getUri();
876 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), URL.encode(path)) {
878 public void onSuccess(Resource result) {
879 updateFolder(folder.getParent(), true, new Command() {
882 public void execute() {
883 folderTreeSelectionModel.setSelected(folder.getParent(), true);
885 if (callback != null)
892 public void onError(Throwable t) {
895 if (t instanceof RestException) {
896 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
897 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
902 displayError("System error unable to delete folder: " + t.getMessage());
906 protected void onUnauthorized(Response response) {
910 deleteFolder.setHeader("X-Auth-Token", getToken());
911 Scheduler.get().scheduleDeferred(deleteFolder);
915 public FolderTreeView getFolderTreeView() {
916 return folderTreeView;
919 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
920 if (iter.hasNext()) {
921 File file = iter.next();
922 String path = targetUri + "/" + file.getName();
923 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
925 public void onSuccess(Resource result) {
926 copyFiles(iter, targetUsername, targetUri, callback);
930 public void onError(Throwable t) {
933 if (t instanceof RestException) {
934 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
937 displayError("System error unable to copy file: "+t.getMessage());
941 protected void onUnauthorized(Response response) {
945 copyFile.setHeader("X-Auth-Token", getToken());
946 copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
947 if (!file.getOwner().equals(targetUsername))
948 copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwner()));
949 copyFile.setHeader("Content-Type", file.getContentType());
950 Scheduler.get().scheduleDeferred(copyFile);
952 else if (callback != null) {
957 public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
958 if (iter.hasNext()) {
959 final Folder f = iter.next();
960 copyFolder(f, targetUsername, targetUri, new Command() {
963 public void execute() {
964 copySubfolders(iter, targetUsername, targetUri, callback);
968 else if (callback != null) {
973 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
974 String path = targetUri + "/" + f.getName();
975 PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
977 public void onSuccess(Resource result) {
978 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
981 public void onSuccess(final Folder _f) {
982 Iterator<File> iter = _f.getFiles().iterator();
983 copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
985 public void execute() {
986 Iterator<Folder> iterf = _f.getSubfolders().iterator();
987 copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
993 public void onError(Throwable t) {
996 if (t instanceof RestException) {
997 displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1000 displayError("System error getting folder: " + t.getMessage());
1004 protected void onUnauthorized(Response response) {
1008 getFolder.setHeader("X-Auth-Token", getToken());
1009 Scheduler.get().scheduleDeferred(getFolder);
1013 public void onError(Throwable t) {
1016 if (t instanceof RestException) {
1017 displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1020 displayError("System error creating folder: " + t.getMessage());
1024 protected void onUnauthorized(Response response) {
1028 createFolder.setHeader("X-Auth-Token", getToken());
1029 createFolder.setHeader("Accept", "*/*");
1030 createFolder.setHeader("Content-Length", "0");
1031 createFolder.setHeader("Content-Type", "application/folder");
1032 Scheduler.get().scheduleDeferred(createFolder);
1035 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1036 selectionModels.add(model);
1039 public OtherSharedTreeView getOtherSharedTreeView() {
1040 return otherSharedTreeView;
1043 public void updateTrash(boolean showFiles, Command callback) {
1044 updateFolder(trash, showFiles, callback, true);
1047 public void updateGroupsNode() {
1048 groupTreeView.updateGroupNode(null);
1051 public void addGroup(String groupname) {
1052 Group newGroup = new Group(groupname);
1053 account.addGroup(newGroup);
1054 groupTreeView.updateGroupNode(null);
1057 public void removeGroup(Group group) {
1058 account.removeGroup(group);
1062 public TreeView getSelectedTree() {
1063 return selectedTree;
1066 public void setSelectedTree(TreeView selected) {
1067 selectedTree = selected;
1070 public Folder getSelection() {
1071 if (selectedTree != null)
1072 return selectedTree.getSelection();
1076 public void showFolderStatistics(int folderFileCount) {
1077 numOfFiles.setHTML(String.valueOf(folderFileCount));
1080 public GroupTreeView getGroupTreeView() {
1081 return groupTreeView;
1084 public void sessionExpired() {
1085 new SessionExpiredDialog(this).center();
1088 public void updateRootFolder(Command callback) {
1089 updateFolder(account.getPithos(), false, callback, true);
1092 void createMySharedTree() {
1093 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1094 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1096 public void onSelectionChange(SelectionChangeEvent event) {
1097 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1098 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1099 upload.setEnabled(false);
1101 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1102 showRelevantToolbarButtons();
1105 if (getSelectedTree().equals(mysharedTreeView))
1106 setSelectedTree(null);
1107 if (getSelectedTree() == null)
1108 showRelevantToolbarButtons();
1112 selectionModels.add(mysharedTreeSelectionModel);
1113 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1114 mysharedTreeViewModel.initialize(new Command() {
1117 public void execute() {
1118 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1119 trees.insert(mysharedTreeView, 2);
1120 treeViews.add(mysharedTreeView);
1121 createOtherSharedTree();
1126 void createOtherSharedTree() {
1127 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1128 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1130 public void onSelectionChange(SelectionChangeEvent event) {
1131 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1132 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1133 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1134 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1135 showRelevantToolbarButtons();
1138 if (getSelectedTree().equals(otherSharedTreeView))
1139 setSelectedTree(null);
1140 if (getSelectedTree() == null)
1141 showRelevantToolbarButtons();
1145 selectionModels.add(otherSharedTreeSelectionModel);
1146 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1147 otherSharedTreeViewModel.initialize(new Command() {
1150 public void execute() {
1151 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1152 trees.insert(otherSharedTreeView, 1);
1153 treeViews.add(otherSharedTreeView);
1158 public native void log1(String message)/*-{
1159 $wnd.console.log(message);
1162 public String getErrorData() {
1164 return error.toString();
1168 public void setError(Throwable t) {
1172 public void showRelevantToolbarButtons() {
1173 toolbar.showRelevantButtons();
1176 public FileUploadDialog getFileUploadDialog() {
1177 if (fileUploadDialog == null)
1178 fileUploadDialog = new FileUploadDialog(this);
1179 return fileUploadDialog;
1182 public void hideUploadIndicator() {
1183 upload.removeStyleName("pithos-uploadButton-loading");
1184 upload.setTitle("");
1187 public void showUploadIndicator() {
1188 upload.addStyleName("pithos-uploadButton-loading");
1189 upload.setTitle("Upload in progress. Click for details.");
1192 public void scheduleFolderHeadCommand(final Folder folder, final Command callback) {
1193 if (folder == null) {
1194 if (callback != null)
1198 HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, getApiPath(), folder.getOwner(), folder.getUri(), folder) {
1201 public void onSuccess(Folder _result) {
1202 if (callback != null)
1207 public void onError(Throwable t) {
1208 if (t instanceof RestException) {
1209 if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
1210 final String path = folder.getUri();
1211 PutRequest newFolder = new PutRequest(getApiPath(), folder.getOwner(), path) {
1213 public void onSuccess(Resource _result) {
1214 scheduleFolderHeadCommand(folder, callback);
1218 public void onError(Throwable _t) {
1221 if(_t instanceof RestException){
1222 displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
1225 displayError("System error creating folder: " + _t.getMessage());
1229 protected void onUnauthorized(Response response) {
1233 newFolder.setHeader("X-Auth-Token", getToken());
1234 newFolder.setHeader("Content-Type", "application/folder");
1235 newFolder.setHeader("Accept", "*/*");
1236 newFolder.setHeader("Content-Length", "0");
1237 Scheduler.get().scheduleDeferred(newFolder);
1240 displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
1243 displayError("System error heading folder: " + t.getMessage());
1245 GWT.log("Error heading folder", t);
1250 protected void onUnauthorized(Response response) {
1254 headFolder.setHeader("X-Auth-Token", getToken());
1255 Scheduler.get().scheduleDeferred(headFolder);
1259 public void scheduleFileHeadCommand(File f, final Command callback) {
1260 HeadRequest<File> headFile = new HeadRequest<File>(File.class, getApiPath(), f.getOwner(), f.getUri(), f) {
1263 public void onSuccess(File _result) {
1264 if (callback != null)
1269 public void onError(Throwable t) {
1270 GWT.log("Error heading file", t);
1272 if (t instanceof RestException)
1273 displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
1275 displayError("System error heading file: " + t.getMessage());
1279 protected void onUnauthorized(Response response) {
1283 headFile.setHeader("X-Auth-Token", getToken());
1284 Scheduler.get().scheduleDeferred(headFile);
1287 public boolean isMySharedSelected() {
1288 return getSelectedTree().equals(getMySharedTreeView());
1291 private FlowPanel createUploadArea() {
1292 FlowPanel area = new FlowPanel();
1293 area.getElement().setId("container");
1294 HTML list = new HTML();
1295 list.getElement().setId("filelist");
1297 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
1300 public void execute() {
1301 setupUploadArea(Pithos.this, getToken());
1308 native void setupUploadArea(Pithos app, String token) /*-{
1309 $wnd.$("#container").pluploadQueue({
1312 unique_names : true,
1313 multiple_queues: true,
1314 drop_element: 'filelist',
1317 Init: function(up, info) {
1318 if ($wnd.console && $wnd.console.log)
1319 $wnd.console.log("Init fired");
1320 up.settings.file_data_name = "X-Object-Data";
1325 FilesAdded: function(up, files) {
1326 $wnd.$('#container').css('border', '');
1327 var api = app.@gr.grnet.pithos.web.client.Pithos::getApiPath()();
1328 var folder = app.@gr.grnet.pithos.web.client.Pithos::getUploadFolder()();
1329 var owner = folder.@gr.grnet.pithos.web.client.foldertree.Folder::getOwner()();
1330 var uri = folder.@gr.grnet.pithos.web.client.foldertree.Folder::getUri()();
1331 var path = api + owner + uri;
1332 for (var j=0; j<files.length; j++) {
1333 files[j].folder = folder;
1334 files[j].url = path + "/" + files[j].name + "?X-Auth-Token=" + encodeURIComponent(token);
1339 BeforeUpload: function(up, file) {
1340 if ($wnd.console && $wnd.console.log)
1341 $wnd.console.log('About to upload ' + file.url);
1342 up.settings.url = file.url;
1345 FileUploaded: function(up, file, response) {
1346 if ($wnd.console && $wnd.console.log) {
1347 $wnd.console.log('File ' + file.name + ' uploaded');
1348 $wnd.console.log('Response: ' + response);
1350 up.removeFile(file);
1351 var folder = app.@gr.grnet.pithos.web.client.Pithos::getUploadFolder()();
1352 if (folder == file.folder)
1353 app.@gr.grnet.pithos.web.client.Pithos::updateUploadFolder()();
1356 UploadComplete: function(up, files) {
1357 if ($wnd.console && $wnd.console.log)
1358 $wnd.console.log('All files finished');
1361 Error: function(up, error) {
1362 if ($wnd.console && $wnd.console.log)
1363 $wnd.console.log("Error occured:" + error);
1367 var uploader = $wnd.$("#container").pluploadQueue();
1368 if (uploader.runtime != 'html5')
1369 $wnd.$("#container").hide();
1371 if ($wnd.console && $wnd.console.log) {
1372 $wnd.console.log(uploader);
1373 $wnd.console.log($wnd.plupload);
1375 $wnd.$('.plupload_header').hide();
1376 $wnd.$('.plupload_filelist_header').hide();
1377 $wnd.$('#container_filelist').css('overflow-y', 'auto');
1378 $wnd.$('.plupload_filelist_footer').hide();
1379 $wnd.document.getElementById('container').addEventListener('dragenter', function(event) {
1380 $wnd.$('#container').css('border', '3px inset');
1382 $wnd.document.getElementById('container').addEventListener('dragleave', function(event) {
1383 $wnd.$('#container').css('border', '');
1388 private Folder getUploadFolder() {
1389 if (folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1390 return getSelection();
1395 private void updateUploadFolder() {
1396 if (folderTreeView.equals(getSelectedTree()) || otherSharedTreeView.equals(getSelectedTree())) {
1397 Folder f = getSelection();
1398 if (getSelectedTree().equals(getFolderTreeView()))
1399 updateFolder(f, true, new Command() {
1402 public void execute() {
1407 updateOtherSharedFolder(f, true);
1411 public native void hideUploadArea() /*-{
1412 $wnd.$("#container").hide();
1415 public native void showUploadArea() /*-{
1416 $wnd.$("#container").show();