2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client;
37 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
38 import gr.grnet.pithos.web.client.foldertree.AccountResource;
39 import gr.grnet.pithos.web.client.foldertree.File;
40 import gr.grnet.pithos.web.client.foldertree.Folder;
41 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
42 import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
43 import gr.grnet.pithos.web.client.foldertree.Resource;
44 import gr.grnet.pithos.web.client.grouptree.Group;
45 import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
46 import gr.grnet.pithos.web.client.grouptree.GroupTreeViewModel;
47 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView;
48 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
49 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
50 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
51 import gr.grnet.pithos.web.client.rest.DeleteRequest;
52 import gr.grnet.pithos.web.client.rest.GetRequest;
53 import gr.grnet.pithos.web.client.rest.HeadRequest;
54 import gr.grnet.pithos.web.client.rest.PutRequest;
55 import gr.grnet.pithos.web.client.rest.RestException;
56 import gr.grnet.pithos.web.client.tagtree.Tag;
57 import gr.grnet.pithos.web.client.tagtree.TagTreeView;
58 import gr.grnet.pithos.web.client.tagtree.TagTreeViewModel;
60 import java.util.ArrayList;
61 import java.util.HashMap;
62 import java.util.Iterator;
63 import java.util.List;
66 import com.google.gwt.core.client.EntryPoint;
67 import com.google.gwt.core.client.GWT;
68 import com.google.gwt.core.client.Scheduler;
69 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
70 import com.google.gwt.event.dom.client.ClickEvent;
71 import com.google.gwt.event.dom.client.ClickHandler;
72 import com.google.gwt.event.logical.shared.ResizeEvent;
73 import com.google.gwt.event.logical.shared.ResizeHandler;
74 import com.google.gwt.http.client.Request;
75 import com.google.gwt.http.client.RequestBuilder;
76 import com.google.gwt.http.client.RequestCallback;
77 import com.google.gwt.http.client.RequestException;
78 import com.google.gwt.http.client.Response;
79 import com.google.gwt.http.client.URL;
80 import com.google.gwt.i18n.client.NumberFormat;
81 import com.google.gwt.json.client.JSONArray;
82 import com.google.gwt.json.client.JSONObject;
83 import com.google.gwt.json.client.JSONParser;
84 import com.google.gwt.json.client.JSONString;
85 import com.google.gwt.json.client.JSONValue;
86 import com.google.gwt.resources.client.ImageResource;
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.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.RootPanel;
100 import com.google.gwt.user.client.ui.VerticalPanel;
101 import com.google.gwt.view.client.SelectionChangeEvent;
102 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
103 import com.google.gwt.view.client.SingleSelectionModel;
106 * Entry point classes define <code>onModuleLoad()</code>.
108 public class Pithos implements EntryPoint, ResizeHandler {
110 public static final String HOME_CONTAINER = "pithos";
112 public static final String TRASH_CONTAINER = "trash";
115 * Instantiate an application-level image bundle. This object will provide
116 * programmatic access to all the images needed by widgets.
118 static Images images = (Images) GWT.create(Images.class);
120 public String getUsername() {
124 public void setAccount(AccountResource acct) {
128 public AccountResource getAccount() {
132 public void updateFolder(Folder f, boolean showfiles, Command callback) {
133 folderTreeView.updateFolder(f, showfiles, callback);
136 public void updateGroupNode(Group group) {
137 groupTreeView.updateGroupNode(group);
140 public void updateSharedFolder(Folder f, boolean showfiles) {
141 mysharedTreeView.updateFolder(f, showfiles);
144 public void updateOtherSharedFolder(Folder f, boolean showfiles) {
145 otherSharedTreeView.updateFolder(f, showfiles);
148 public List<Tag> getAllTags() {
149 List<Tag> tagList = new ArrayList<Tag>();
150 for (Folder f : account.getContainers()) {
151 for (String t : f.getTags()) {
152 tagList.add(new Tag(t));
158 public MysharedTreeView getMySharedTreeView() {
159 return mysharedTreeView;
163 * An aggregate image bundle that pulls together all the images for this
164 * application into a single bundle.
166 public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
168 @Source("gr/grnet/pithos/resources/document.png")
169 ImageResource folders();
171 @Source("gr/grnet/pithos/resources/advancedsettings.png")
172 ImageResource tools();
176 * The Application Clipboard implementation;
178 private Clipboard clipboard = new Clipboard();
181 * The top panel that contains the menu bar.
183 private TopPanel topPanel;
186 * The panel that contains the various system messages.
188 private MessagePanel messagePanel = new MessagePanel(Pithos.images);
191 * The bottom panel that contains the status bar.
193 private StatusPanel statusPanel = null;
196 * The file list widget.
198 private FileList fileList;
201 * The tab panel that occupies the right side of the screen.
203 private VerticalPanel inner = new VerticalPanel();
207 * The split panel that will contain the left and right panels.
209 private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
212 * The currently selected item in the application, for use by the Edit menu
213 * commands. Potential types are Folder, File, User and Group.
215 private Object currentSelection;
217 public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
219 private String username = null;
222 * The authentication token of the current user.
224 private String token;
228 SingleSelectionModel<Folder> folderTreeSelectionModel;
229 FolderTreeViewModel folderTreeViewModel;
230 FolderTreeView folderTreeView;
232 SingleSelectionModel<Folder> mysharedTreeSelectionModel;
233 MysharedTreeViewModel mysharedTreeViewModel;
234 MysharedTreeView mysharedTreeView = null;;
236 protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
237 OtherSharedTreeViewModel otherSharedTreeViewModel;
238 OtherSharedTreeView otherSharedTreeView = null;
240 GroupTreeViewModel groupTreeViewModel;
241 private GroupTreeView groupTreeView;
243 private TreeView selectedTree;
244 protected AccountResource account;
248 @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
252 private HTML totalFiles;
254 private HTML usedBytes;
256 private HTML totalBytes;
258 private HTML usedPercent;
260 private HTML numOfFiles;
262 private Button toolsButton;
265 public void onModuleLoad() {
266 if (parseUserCredentials())
270 private void initialize() {
271 boolean bareContent = Window.Location.getParameter("noframe") != null;
272 String contentWidth = bareContent ? "100%" : "75%";
274 VerticalPanel outer = new VerticalPanel();
275 outer.setWidth("100%");
277 outer.addStyleName("pithos-outer");
281 topPanel = new TopPanel(this, Pithos.images);
282 topPanel.setWidth("100%");
284 outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
287 messagePanel.setWidth(contentWidth);
288 messagePanel.setVisible(false);
289 outer.add(messagePanel);
290 outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
292 HorizontalPanel header = new HorizontalPanel();
293 header.addStyleName("pithos-header");
294 header.setWidth(contentWidth);
295 HorizontalPanel leftHeader = new HorizontalPanel();
296 VerticalPanel uploadButtonPanel = new VerticalPanel();
297 upload = new Button("Upload File", new ClickHandler() {
299 public void onClick(@SuppressWarnings("unused") ClickEvent event) {
300 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
303 upload.addStyleName("pithos-uploadButton");
304 uploadButtonPanel.add(upload);
305 uploadButtonPanel.setWidth("100%");
306 uploadButtonPanel.setHeight("60px");
307 uploadButtonPanel.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
308 uploadButtonPanel.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
309 leftHeader.add(uploadButtonPanel);
310 header.add(leftHeader);
311 header.setCellWidth(leftHeader, "35%");
313 HorizontalPanel rightHeader = new HorizontalPanel();
314 rightHeader.addStyleName("pithos-rightSide");
315 rightHeader.setSpacing(5);
317 toolsButton = new Button(AbstractImagePrototype.create(images.tools()).getHTML());
318 toolsButton.addClickHandler(new ClickHandler() {
321 public void onClick(ClickEvent event) {
322 ToolsMenu menu = new ToolsMenu(Pithos.this, images, getSelectedTree(), getSelectedTree().getSelection(), getFileList().getSelectedFiles());
323 if (!menu.isEmpty()) {
324 menu.setPopupPosition(event.getClientX(), event.getClientY());
329 rightHeader.add(toolsButton);
330 rightHeader.setCellHorizontalAlignment(toolsButton, HasHorizontalAlignment.ALIGN_LEFT);
332 HorizontalPanel folderStatistics = new HorizontalPanel();
333 folderStatistics.addStyleName("pithos-folderStatistics");
334 numOfFiles = new HTML();
335 folderStatistics.add(numOfFiles);
336 folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
337 HTML numOfFilesLabel = new HTML(" Files");
338 folderStatistics.add(numOfFilesLabel);
339 folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
340 rightHeader.add(folderStatistics);
341 rightHeader.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
342 header.add(rightHeader);
343 header.setCellVerticalAlignment(rightHeader, HasVerticalAlignment.ALIGN_MIDDLE);
344 header.setCellHeight(rightHeader, "60px");
346 outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
347 // Inner contains the various lists.nner
348 inner.sinkEvents(Event.ONCONTEXTMENU);
349 inner.setWidth("100%");
351 folderTreeSelectionModel = new SingleSelectionModel<Folder>();
352 folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
354 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
355 if (folderTreeSelectionModel.getSelectedObject() != null) {
356 deselectOthers(folderTreeView, folderTreeSelectionModel);
357 applyPermissions(folderTreeSelectionModel.getSelectedObject());
358 folderTreeView.addStyleName("cellTreeWidget-selectedTree");
359 Folder f = folderTreeSelectionModel.getSelectedObject();
363 folderTreeView.removeStyleName("cellTreeWidget-selectedTree");
366 selectionModels.add(folderTreeSelectionModel);
368 folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
369 folderTreeView = new FolderTreeView(folderTreeViewModel);
371 fileList = new FileList(this, images, folderTreeView);
374 groupTreeViewModel = new GroupTreeViewModel(this);
375 groupTreeView = new GroupTreeView(groupTreeViewModel);
377 trees = new VerticalPanel();
378 trees.setWidth("100%");
381 HorizontalPanel treeHeader = new HorizontalPanel();
382 treeHeader.addStyleName("pithos-treeHeader");
383 treeHeader.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
384 treeHeader.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
385 HorizontalPanel statistics = new HorizontalPanel();
386 statistics.addStyleName("pithos-statistics");
387 statistics.add(new HTML("Total Objects: "));
388 totalFiles = new HTML();
389 statistics.add(totalFiles);
390 statistics.add(new HTML(" | Used: "));
391 usedBytes = new HTML();
392 statistics.add(usedBytes);
393 statistics.add(new HTML(" of "));
394 totalBytes = new HTML();
395 statistics.add(totalBytes);
396 statistics.add(new HTML(" ("));
397 usedPercent = new HTML();
398 statistics.add(usedPercent);
399 statistics.add(new HTML(")"));
400 treeHeader.add(statistics);
401 treeHeader.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_LEFT);
402 trees.add(treeHeader);
404 trees.add(folderTreeView);
405 trees.add(groupTreeView);
406 // Add the left and right panels to the split panel.
407 splitPanel.setLeftWidget(trees);
408 splitPanel.setRightWidget(inner);
409 splitPanel.setSplitPosition("35%");
410 splitPanel.setSize("100%", "100%");
411 splitPanel.addStyleName("pithos-splitPanel");
412 splitPanel.setWidth(contentWidth);
413 outer.add(splitPanel);
414 outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
417 statusPanel = new StatusPanel();
418 statusPanel.setWidth("100%");
419 outer.add(statusPanel);
420 outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
423 // Hook the window resize event, so that we can adjust the UI.
424 Window.addResizeHandler(this);
425 // Clear out the window's built-in margin, because we want to take
426 // advantage of the entire client area.
427 Window.setMargin("0px");
428 // Finally, add the outer panel to the RootPanel, so that it will be
430 RootPanel.get().add(outer);
431 // Call the window resized handler to get the initial sizes setup. Doing
432 // this in a deferred command causes it to occur after all widgets'
433 // sizes have been computed by the browser.
434 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
437 public void execute() {
438 onWindowResized(Window.getClientHeight());
442 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
444 public void execute() {
445 fetchAccount(new Command() {
448 public void execute() {
449 if (!account.hasHomeContainer())
450 createHomeContainer(account, this);
451 else if (!account.hasTrashContainer())
452 createTrashContainer(this);
454 for (Folder f : account.getContainers())
455 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
459 folderTreeViewModel.initialize(account, new Command() {
462 public void execute() {
463 createMySharedTree();
466 groupTreeViewModel.initialize();
475 public void applyPermissions(Folder f) {
478 upload.setEnabled(false);
480 Boolean[] perms = f.getPermissions().get(username);
481 if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
482 upload.setEnabled(true);
485 upload.setEnabled(false);
489 upload.setEnabled(false);
492 @SuppressWarnings({ "rawtypes", "unchecked" })
493 public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
494 selectedTree = _selectedTree;
495 for (SingleSelectionModel s : selectionModels)
496 if (!s.equals(model))
497 s.setSelected(s.getSelectedObject(), false);
500 public void showFiles(Folder f) {
501 Set<File> files = f.getFiles();
505 public void showFiles(Set<File> files) {
506 //Iterator<File> iter = files.iterator();
507 //fetchFile(iter, files);
508 fileList.setFiles(new ArrayList<File>(files));
511 protected void fetchFile(final Iterator<File> iter, final Set<File> files) {
512 if (iter.hasNext()) {
513 File file = iter.next();
514 String path = file.getUri() + "?format=json";
515 GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
517 public void onSuccess(@SuppressWarnings("unused") File _result) {
518 fetchFile(iter, files);
522 public void onError(Throwable t) {
523 GWT.log("Error getting file", t);
524 if (t instanceof RestException)
525 displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
527 displayError("System error fetching file: " + t.getMessage());
531 protected void onUnauthorized(Response response) {
535 getFile.setHeader("X-Auth-Token", "0000");
536 Scheduler.get().scheduleDeferred(getFile);
539 fileList.setFiles(new ArrayList<File>(files));
543 * Parse and store the user credentials to the appropriate fields.
545 private boolean parseUserCredentials() {
546 username = Window.Location.getParameter("user");
547 token = Window.Location.getParameter("token");
548 Configuration conf = (Configuration) GWT.create(Configuration.class);
549 if (username == null || username.length() == 0 || token == null || token.length() == 0) {
550 String cookie = conf.authCookie();
551 String auth = Cookies.getCookie(cookie);
556 String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
557 if (authSplit.length != 2) {
561 username = authSplit[0];
562 token = authSplit[1];
565 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token);
570 * Redirect the user to the login page for authentication.
572 protected void authenticateUser() {
573 Configuration conf = (Configuration) GWT.create(Configuration.class);
574 Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
577 protected void fetchAccount(final Command callback) {
578 String path = "?format=json";
580 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
582 public void onSuccess(AccountResource _result) {
584 if (callback != null)
589 public void onError(Throwable t) {
590 GWT.log("Error getting account", t);
591 if (t instanceof RestException)
592 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
594 displayError("System error fetching user data: " + t.getMessage());
598 protected void onUnauthorized(Response response) {
602 getAccount.setHeader("X-Auth-Token", token);
603 Scheduler.get().scheduleDeferred(getAccount);
606 public void updateStatistics() {
607 HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
610 public void onSuccess(@SuppressWarnings("unused") AccountResource _result) {
615 public void onError(Throwable t) {
616 GWT.log("Error getting account", t);
617 if (t instanceof RestException)
618 displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
620 displayError("System error fetching user data: " + t.getMessage());
624 protected void onUnauthorized(Response response) {
628 headAccount.setHeader("X-Auth-Token", token);
629 Scheduler.get().scheduleDeferred(headAccount);
632 protected void showStatistics() {
633 totalFiles.setHTML(String.valueOf(account.getNumberOfObjects()));
634 usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
635 totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
636 NumberFormat nf = NumberFormat.getPercentFormat();
637 usedPercent.setHTML(nf.format(account.getUsedPercentage()));
640 protected void createHomeContainer(final AccountResource _account, final Command callback) {
641 String path = "/" + Pithos.HOME_CONTAINER;
642 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
644 public void onSuccess(@SuppressWarnings("unused") Resource result) {
645 if (!_account.hasTrashContainer())
646 createTrashContainer(callback);
648 fetchAccount(callback);
652 public void onError(Throwable t) {
653 GWT.log("Error creating pithos", t);
654 if (t instanceof RestException)
655 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
657 displayError("System error Error creating pithos: " + t.getMessage());
661 protected void onUnauthorized(Response response) {
665 createPithos.setHeader("X-Auth-Token", getToken());
666 Scheduler.get().scheduleDeferred(createPithos);
669 protected void createTrashContainer(final Command callback) {
670 String path = "/" + Pithos.TRASH_CONTAINER;
671 PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
673 public void onSuccess(@SuppressWarnings("unused") Resource result) {
674 fetchAccount(callback);
678 public void onError(Throwable t) {
679 GWT.log("Error creating pithos", t);
680 if (t instanceof RestException)
681 displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
683 displayError("System error Error creating pithos: " + t.getMessage());
687 protected void onUnauthorized(Response response) {
691 createPithos.setHeader("X-Auth-Token", getToken());
692 Scheduler.get().scheduleDeferred(createPithos);
696 * Creates an HTML fragment that places an image & caption together, for use
699 * @param imageProto an image prototype for an image
700 * @param caption the group caption
701 * @return the header HTML fragment
703 private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
704 String captionHTML = "<table class='caption' cellpadding='0' "
705 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML()
706 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'> "
707 + caption + "</b></td></tr></table>";
711 protected void onWindowResized(int height) {
712 // Adjust the split panel to take up the available room in the window.
713 int newHeight = height - splitPanel.getAbsoluteTop() - 60;
716 splitPanel.setHeight("" + newHeight);
717 inner.setHeight("" + newHeight);
721 public void onResize(ResizeEvent event) {
722 int height = event.getHeight();
723 onWindowResized(height);
727 * Display an error message.
729 * @param msg the message to display
731 public void displayError(String msg) {
732 messagePanel.displayError(msg);
736 * Display a warning message.
738 * @param msg the message to display
740 public void displayWarning(String msg) {
741 messagePanel.displayWarning(msg);
745 * Display an informational message.
747 * @param msg the message to display
749 public void displayInformation(String msg) {
750 messagePanel.displayInformation(msg);
754 * Retrieve the fileList.
756 * @return the fileList
758 public FileList getFileList() {
763 * Retrieve the topPanel.
765 * @return the topPanel
767 TopPanel getTopPanel() {
772 * Retrieve the clipboard.
774 * @return the clipboard
776 public Clipboard getClipboard() {
780 public StatusPanel getStatusPanel() {
784 public String getToken() {
788 public static native void preventIESelection() /*-{
789 $doc.body.onselectstart = function () { return false; };
792 public static native void enableIESelection() /*-{
793 if ($doc.body.onselectstart != null)
794 $doc.body.onselectstart = null;
798 * @return the absolute path of the API root URL
800 public String getApiPath() {
801 Configuration conf = (Configuration) GWT.create(Configuration.class);
802 return conf.apiPath();
806 * History support for folder navigation
807 * adds a new browser history entry
811 public void updateHistory(String key){
812 // Replace any whitespace of the initial string to "+"
813 // String result = key.replaceAll("\\s","+");
814 // Add a new browser history entry.
815 // History.newItem(result);
816 History.newItem(key);
819 public void deleteFolder(final Folder folder) {
820 String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
821 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
822 builder.setHeader("X-Auth-Token", getToken());
824 builder.sendRequest("", new RequestCallback() {
826 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
827 if (response.getStatusCode() == Response.SC_OK) {
828 JSONValue json = JSONParser.parseStrict(response.getText());
829 JSONArray array = json.isArray();
832 deleteObject(folder, i, array);
838 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
839 displayError("System error unable to delete folder: " + exception.getMessage());
843 catch (RequestException e) {
847 void deleteObject(final Folder folder, final int i, final JSONArray array) {
848 if (i < array.size()) {
849 JSONObject o = array.get(i).isObject();
850 if (o != null && !o.containsKey("subdir")) {
851 JSONString name = o.get("name").isString();
852 String path = "/" + folder.getContainer() + "/" + name.stringValue();
853 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), path) {
855 public void onSuccess(@SuppressWarnings("unused") Resource result) {
856 deleteObject(folder, i + 1, array);
860 public void onError(Throwable t) {
862 displayError("System error unable to delete folder: " + t.getMessage());
866 protected void onUnauthorized(Response response) {
870 delete.setHeader("X-Auth-Token", getToken());
871 Scheduler.get().scheduleDeferred(delete);
873 else if (o != null) {
874 String subdir = o.get("subdir").isString().stringValue();
875 subdir = subdir.substring(0, subdir.length() - 1);
876 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
877 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
878 builder.setHeader("X-Auth-Token", getToken());
880 builder.sendRequest("", new RequestCallback() {
882 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
883 if (response.getStatusCode() == Response.SC_OK) {
884 JSONValue json = JSONParser.parseStrict(response.getText());
885 JSONArray array2 = json.isArray();
886 if (array2 != null) {
887 int l = array.size();
888 for (int j=0; j<array2.size(); j++) {
889 array.set(l++, array2.get(j));
892 deleteObject(folder, i + 1, array);
897 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
898 displayError("System error unable to delete folder: " + exception.getMessage());
902 catch (RequestException e) {
907 String path = folder.getUri();
908 DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
910 public void onSuccess(@SuppressWarnings("unused") Resource result) {
911 updateFolder(folder.getParent(), true, new Command() {
914 public void execute() {
921 public void onError(Throwable t) {
923 if (t instanceof RestException) {
924 if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
925 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
930 displayError("System error unable to delete folder: " + t.getMessage());
934 protected void onUnauthorized(Response response) {
938 deleteFolder.setHeader("X-Auth-Token", getToken());
939 Scheduler.get().scheduleDeferred(deleteFolder);
943 public FolderTreeView getFolderTreeView() {
944 return folderTreeView;
947 public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
948 if (iter.hasNext()) {
949 File file = iter.next();
950 String path = targetUri + "/" + file.getName();
951 PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
953 public void onSuccess(@SuppressWarnings("unused") Resource result) {
954 copyFiles(iter, targetUsername, targetUri, callback);
958 public void onError(Throwable t) {
960 if (t instanceof RestException) {
961 displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
964 displayError("System error unable to copy file: "+t.getMessage());
968 protected void onUnauthorized(Response response) {
972 copyFile.setHeader("X-Auth-Token", getToken());
973 copyFile.setHeader("X-Copy-From", file.getUri());
974 if (!file.getOwner().equals(targetUsername))
975 copyFile.setHeader("X-Source-Account", file.getOwner());
976 Scheduler.get().scheduleDeferred(copyFile);
978 else if (callback != null) {
983 public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
984 if (iter.hasNext()) {
985 final Folder f = iter.next();
986 copyFolder(f, targetUsername, targetUri, new Command() {
989 public void execute() {
990 copySubfolders(iter, targetUsername, targetUri, callback);
994 else if (callback != null) {
999 public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
1000 String path = targetUri + "/" + f.getName();
1001 PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
1003 public void onSuccess(@SuppressWarnings("unused") Resource result) {
1004 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
1007 public void onSuccess(final Folder _f) {
1008 Iterator<File> iter = _f.getFiles().iterator();
1009 copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
1011 public void execute() {
1012 Iterator<Folder> iterf = _f.getSubfolders().iterator();
1013 copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1019 public void onError(Throwable t) {
1021 if (t instanceof RestException) {
1022 displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1025 displayError("System error getting folder: " + t.getMessage());
1029 protected void onUnauthorized(Response response) {
1033 getFolder.setHeader("X-Auth-Token", getToken());
1034 Scheduler.get().scheduleDeferred(getFolder);
1038 public void onError(Throwable t) {
1040 if (t instanceof RestException) {
1041 displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1044 displayError("System error creating folder: " + t.getMessage());
1048 protected void onUnauthorized(Response response) {
1052 createFolder.setHeader("X-Auth-Token", getToken());
1053 createFolder.setHeader("Accept", "*/*");
1054 createFolder.setHeader("Content-Length", "0");
1055 createFolder.setHeader("Content-Type", "application/folder");
1056 Scheduler.get().scheduleDeferred(createFolder);
1059 public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1060 selectionModels.add(model);
1063 public OtherSharedTreeView getOtherSharedTreeView() {
1064 return otherSharedTreeView;
1067 public void updateTrash(boolean showFiles, Command callback) {
1068 updateFolder(trash, showFiles, callback);
1071 public void updateGroupsNode() {
1072 groupTreeView.updateGroupNode(null);
1075 public void addGroup(String groupname) {
1076 Group newGroup = new Group(groupname);
1077 account.addGroup(newGroup);
1078 groupTreeView.updateGroupNode(null);
1081 public void removeGroup(Group group) {
1082 account.removeGroup(group);
1086 public TreeView getSelectedTree() {
1087 return selectedTree;
1090 public Folder getSelection() {
1091 return selectedTree.getSelection();
1094 public void showFolderStatistics(int folderFileCount) {
1095 numOfFiles.setHTML(String.valueOf(folderFileCount));
1098 public GroupTreeView getGroupTreeView() {
1099 return groupTreeView;
1102 public void sessionExpired() {
1103 new SessionExpiredDialog(this).center();
1106 public void updateRootFolder(Command callback) {
1107 updateFolder(account.getPithos(), false, callback);
1110 void createMySharedTree() {
1111 mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1112 mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1114 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
1115 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1116 deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1117 upload.setEnabled(false);
1118 updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1119 mysharedTreeView.addStyleName("cellTreeWidget-selectedTree");
1122 mysharedTreeView.removeStyleName("cellTreeWidget-selectedTree");
1125 selectionModels.add(mysharedTreeSelectionModel);
1126 mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1127 mysharedTreeViewModel.initialize(new Command() {
1130 public void execute() {
1131 mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1132 trees.insert(mysharedTreeView, 3);
1133 createOtherSharedTree();
1138 void createOtherSharedTree() {
1139 otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1140 otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1142 public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
1143 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1144 deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1145 applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1146 updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1147 otherSharedTreeView.addStyleName("cellTreeWidget-selectedTree");
1150 otherSharedTreeView.removeStyleName("cellTreeWidget-selectedTree");
1153 selectionModels.add(otherSharedTreeSelectionModel);
1154 otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1155 otherSharedTreeViewModel.initialize(new Command() {
1158 public void execute() {
1159 otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1160 trees.insert(otherSharedTreeView, 4);
1165 public void logoff() {
1166 Configuration conf = (Configuration) GWT.create(Configuration.class);
1167 Cookies.removeCookie(conf.authCookie());
1168 Cookies.removeCookie(conf.authTokenCookie(), "/");
1169 for (String s: Cookies.getCookieNames())
1170 if (s.startsWith(conf.shibSessionCookiePrefix()))
1171 Cookies.removeCookie(s, "/");
1172 Window.Location.assign(Window.Location.getPath());