Don't decode resource names in JSON values. This is only necessary when names are...
[pithos] / src / gr / ebs / gss / client / GSS.java
index fe48e7e..af95cac 100644 (file)
@@ -1,13 +1,20 @@
 /*
- * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd. This file is part
- * of GSS. GSS is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version. GSS is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details. You should have received a copy of the GNU General Public License
- * along with GSS. If not, see <http://www.gnu.org/licenses/>.
+ * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.
+ *
+ * This file is part of GSS.
+ *
+ * GSS is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GSS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
  */
 package gr.ebs.gss.client;
 
@@ -21,6 +28,7 @@ import gr.ebs.gss.client.rest.resource.FolderResource;
 import gr.ebs.gss.client.rest.resource.TrashResource;
 import gr.ebs.gss.client.rest.resource.UserResource;
 
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 
@@ -33,6 +41,8 @@ import com.google.gwt.event.logical.shared.ResizeEvent;
 import com.google.gwt.event.logical.shared.ResizeHandler;
 import com.google.gwt.event.logical.shared.SelectionEvent;
 import com.google.gwt.event.logical.shared.SelectionHandler;
+import com.google.gwt.event.logical.shared.ValueChangeEvent;
+import com.google.gwt.event.logical.shared.ValueChangeHandler;
 import com.google.gwt.resources.client.ClientBundle;
 import com.google.gwt.resources.client.ImageResource;
 import com.google.gwt.user.client.Command;
@@ -193,6 +203,7 @@ public class GSS implements EntryPoint, ResizeHandler {
 
        private PickupDragController dragController;
 
+       @Override
        public void onModuleLoad() {
                // Initialize the singleton before calling the constructors of the
                // various widgets that might call GSS.get().
@@ -286,42 +297,54 @@ public class GSS implements EntryPoint, ResizeHandler {
                        @Override
                        public void onSelection(SelectionEvent<Integer> event) {
                                int tabIndex = event.getSelectedItem();
+//                             TreeItem treeItem = GSS.get().getFolders().getCurrent();
                                switch (tabIndex) {
                                        case 0:
+//                                             Files tab selected
                                                fileList.clearSelectedRows();
                                                fileList.updateCurrentlyShowingStats();
-                                               History.newItem("Files");
                                                break;
                                        case 1:
+//                                             Groups tab selected
                                                groups.updateCurrentlyShowingStats();
-                                               History.newItem("Groups");
+                                       updateHistory("Groups");
                                                break;
                                        case 2:
+//                                             Search tab selected
                                                searchResults.clearSelectedRows();
                                                searchResults.updateCurrentlyShowingStats();
-                                               History.newItem("Search");
+                                       updateHistory("Search");
                                                break;
                                }
                        }
                });
-//             History.addValueChangeHandler(new ValueChangeHandler<String>() {
-//
-//                     public void onValueChange(ValueChangeEvent<String> event) {
-//                             String historyToken = event.getValue();
-//                             try {
-//                                     if (historyToken.equals("Files"))
-//                                             inner.selectTab(1);
-//                                     else if (historyToken.equals("Groups"))
-//                                             inner.selectTab(2);
-//                                     else if (historyToken.equals("Search Results"))
-//                                             inner.selectTab(3);
-//                                     else
-//                                             inner.selectTab(0);
-//                             } catch (IndexOutOfBoundsException e) {
-//                                     inner.selectTab(0);
-//                             }
-//                     }
-//             });
+//             If the application starts with no history token, redirect to a new "Files" state
+               String initToken = History.getToken();
+               if(initToken.length() == 0)
+                       History.newItem("Files");
+//                Add history listener to handle any history events
+               History.addValueChangeHandler(new ValueChangeHandler<String>() {
+                       @Override
+                       public void onValueChange(ValueChangeEvent<String> event) {
+                               String tokenInput = event.getValue();
+                               String historyToken = handleSpecialFolderNames(tokenInput);
+                               try {
+                                       if(historyToken.equals("Search"))
+                                               inner.selectTab(2);
+                                       else if(historyToken.equals("Groups"))
+                                               inner.selectTab(1);
+                                       else if(historyToken.equals("Files")|| historyToken.length()==0)
+                                               inner.selectTab(0);
+                                       else {
+                                               PopupTree popupTree = GSS.get().getFolders().getPopupTree();
+                                               TreeItem treeObj = GSS.get().getFolders().getPopupTree().getTreeItem(historyToken);
+                                               SelectionEvent.fire(popupTree, treeObj);
+                                               }
+                                       } catch (IndexOutOfBoundsException e) {
+                                               inner.selectTab(0);
+                                               }
+                                       }
+                       });
 
                // Add the left and right panels to the split panel.
                splitPanel.setLeftWidget(folders);
@@ -357,6 +380,7 @@ public class GSS implements EntryPoint, ResizeHandler {
                // sizes have been computed by the browser.
                DeferredCommand.addCommand(new Command() {
 
+                       @Override
                        public void execute() {
                                onWindowResized(Window.getClientHeight());
                        }
@@ -379,6 +403,7 @@ public class GSS implements EntryPoint, ResizeHandler {
                                if (announcement != null)
                                        DeferredCommand.addCommand(new Command() {
 
+                                               @Override
                                                public void execute() {
                                                        displayInformation(announcement);
                                                }
@@ -405,9 +430,6 @@ public class GSS implements EntryPoint, ResizeHandler {
                Configuration conf = (Configuration) GWT.create(Configuration.class);
                String cookie = conf.authCookie();
                String auth = Cookies.getCookie(cookie);
-               String domain = Window.Location.getHostName();
-               String path = Window.Location.getPath();
-               Cookies.setCookie(cookie, "", null, domain, path, false);
                if (auth == null) {
                        authenticateUser();
                        // Redundant, but silences warnings about possible auth NPE, below.
@@ -416,7 +438,7 @@ public class GSS implements EntryPoint, ResizeHandler {
                int sepIndex = auth.indexOf(conf.cookieSeparator());
                if (sepIndex == -1)
                        authenticateUser();
-               token = auth.substring(sepIndex + 1, auth.length());
+               token = auth.substring(sepIndex + 1);
                final String username = auth.substring(0, sepIndex);
                if (username == null)
                        authenticateUser();
@@ -425,6 +447,7 @@ public class GSS implements EntryPoint, ResizeHandler {
 
                DeferredCommand.addCommand(new Command() {
 
+                       @Override
                        public void execute() {
                                fetchUser(username);
                        }
@@ -440,10 +463,14 @@ public class GSS implements EntryPoint, ResizeHandler {
        }
 
        /**
-        * Redirect the user to the logout page.
+        * Clear the cookie and redirect the user to the logout page.
         */
        void logout() {
                Configuration conf = (Configuration) GWT.create(Configuration.class);
+               String cookie = conf.authCookie();
+               String domain = Window.Location.getHostName();
+               String path = Window.Location.getPath();
+               Cookies.setCookie(cookie, "", null, domain, path, false);
                Window.Location.assign(conf.logoutUrl());
        }
 
@@ -560,8 +587,8 @@ public class GSS implements EntryPoint, ResizeHandler {
         * @param y the new height
         */
        public static native void resizeTo(int x, int y) /*-{
-                                                                                                               $wnd.resizeTo(x,y);
-                                                                                                               }-*/;
+               $wnd.resizeTo(x,y);
+       }-*/;
 
        /**
         * A helper method that returns true if the user's list is currently visible
@@ -729,13 +756,13 @@ public class GSS implements EntryPoint, ResizeHandler {
        }
 
        public static native void preventIESelection() /*-{
-                                                                                                       $doc.body.onselectstart = function () { return false; };
-                                                                                                       }-*/;
+               $doc.body.onselectstart = function () { return false; };
+       }-*/;
 
        public static native void enableIESelection() /*-{
-                                                                                                       if ($doc.body.onselectstart != null)
-                                                                                                       $doc.body.onselectstart = null;
-                                                                                                       }-*/;
+               if ($doc.body.onselectstart != null)
+               $doc.body.onselectstart = null;
+       }-*/;
 
        /**
         * @return the absolute path of the API root URL
@@ -754,4 +781,44 @@ public class GSS implements EntryPoint, ResizeHandler {
                Cookies.setCookie(cookie, "", null, domain, path, false);
        }
 
+       /**
+        * History support for folder navigation
+        * adds a new browser history entry
+        *
+        * @param key
+        */
+       public void updateHistory(String key){
+//             Replace any whitespace of the initial string to "+"
+//             String result = key.replaceAll("\\s","+");
+//             Add a new browser history entry.
+//             History.newItem(result);
+               History.newItem(key);
+       }
+
+       /**
+        * This method examines the token input and add a "/" at the end in case it's omitted.
+        * This happens only in Files/trash/, Files/shared/, Files/others.
+        *
+        * @param tokenInput
+        * @return the formated token with a "/" at the end or the same tokenInput parameter
+        */
+
+       private String handleSpecialFolderNames(String tokenInput){
+               List<String> pathsToCheck = Arrays.asList("Files/trash", "Files/shared", "Files/others");
+               if(pathsToCheck.contains(tokenInput))
+                       return tokenInput + "/";
+               return tokenInput;
+
+       }
+
+       /**
+        * Reject illegal resource names, like '.' or '..'.
+        */
+       static boolean isValidResourceName(String name) {
+               if (".".equals(name) || "..".equals(name))
+                       return false;
+               return true;
+       }
+
+
 }