Added ids in homefolder(tree.homeFolder), Trash(tree.Trash), My Shared (tree.myShared...
[pithos] / src / gr / ebs / gss / client / GSS.java
index e2b9f96..b3f53c9 100644 (file)
@@ -19,6 +19,7 @@
 package gr.ebs.gss.client;
 
 import gr.ebs.gss.client.clipboard.Clipboard;
+import gr.ebs.gss.client.commands.GetUserCommand;
 import gr.ebs.gss.client.dnd.DnDFocusPanel;
 import gr.ebs.gss.client.dnd.DnDSimpleFocusPanel;
 import gr.ebs.gss.client.rest.GetCommand;
@@ -28,10 +29,11 @@ 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.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
 import com.allen_sauer.gwt.dnd.client.DragContext;
 import com.allen_sauer.gwt.dnd.client.PickupDragController;
@@ -44,7 +46,7 @@ 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.http.client.URL;
+import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.resources.client.ClientBundle;
 import com.google.gwt.resources.client.ImageResource;
 import com.google.gwt.user.client.Command;
@@ -199,18 +201,15 @@ public class GSS implements EntryPoint, ResizeHandler {
        private String token;
 
        /**
-        * A map that stores user's selection in order browser's
-        * history functionality to be implemented.
-        */
-       private Map<String, Object> map = new HashMap<String, Object>();
-
-       /**
         * The WebDAV password of the current user
         */
        private String webDAVPassword;
 
        private PickupDragController dragController;
 
+       public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
+
+       @Override
        public void onModuleLoad() {
                // Initialize the singleton before calling the constructors of the
                // various widgets that might call GSS.get().
@@ -221,37 +220,52 @@ public class GSS implements EntryPoint, ResizeHandler {
 
                        @Override
                        public void previewDragStart() throws VetoDragException {
-                               super.previewDragStart();
-                               if (context.selectedWidgets.isEmpty())
+                           super.previewDragStart();
+                           if (context.selectedWidgets.isEmpty())
                                        throw new VetoDragException();
 
-                               if (context.draggable != null)
-                                       if (context.draggable instanceof DnDFocusPanel) {
+                           if(context.draggable != null)
+                                       if(context.draggable instanceof DnDFocusPanel){
                                                DnDFocusPanel toDrop = (DnDFocusPanel) context.draggable;
                                                // prevent drag and drop for trashed files and for
                                                // unselected tree items
-                                               if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
+                                               if(toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
                                                        throw new VetoDragException();
-                                               else if (toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
+                                               else if(toDrop.getItem() != null && !toDrop.getItem().equals(folders.getCurrent()))
                                                        throw new VetoDragException();
-                                               else if (toDrop.getItem() != null && !toDrop.getItem().isDraggable())
+                                               else if(toDrop.getItem() != null && !toDrop.getItem().isDraggable())
                                                        throw new VetoDragException();
 
                                        } else if (context.draggable instanceof DnDSimpleFocusPanel) {
-                                               DnDSimpleFocusPanel toDrop = (DnDSimpleFocusPanel) context.draggable;
+                                       DnDSimpleFocusPanel toDrop = (DnDSimpleFocusPanel) context.draggable;
                                                // prevent drag and drop for trashed files and for
                                                // unselected tree items
-                                               if (toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
+                                               if(toDrop.getFiles() != null && folders.isTrashItem(folders.getCurrent()))
                                                        throw new VetoDragException();
-                                       }
-                       }
+                               }
+                         }
 
                        @Override
                        protected Widget newDragProxy(DragContext aContext) {
                                AbsolutePanel container = new AbsolutePanel();
+                               HTML html = null;
                                DOM.setStyleAttribute(container.getElement(), "overflow", "visible");
+                               if(aContext.draggable!=null && aContext.draggable.getParent()!= null && aContext.draggable.getParent() instanceof FileTable){
+                                       if(getFileList().getSelectedFiles().size()>1){
+                                               html=new HTML(getFileList().getSelectedFiles().size()+ " files");
+                                               container.add(html);
+                                               return container;
+                                       }
+                                       FileTable proxy;
+                                   proxy = new FileTable(1,8);
+                                   proxy.addStyleName("gss-List");
+                                   FileTable draggableTable = (FileTable) context.draggable.getParent();
+                                   int dragRow = FileTable.getWidgetRow(context.draggable, draggableTable);
+                                   FileTable.copyRow(draggableTable, proxy, dragRow, 0);
+                                   return proxy;
+
+                               }
                                for (Iterator iterator = aContext.selectedWidgets.iterator(); iterator.hasNext();) {
-                                       HTML html = null;
                                        Widget widget = (Widget) iterator.next();
                                        if (widget instanceof DnDFocusPanel) {
                                                DnDFocusPanel book = (DnDFocusPanel) widget;
@@ -260,10 +274,11 @@ public class GSS implements EntryPoint, ResizeHandler {
                                                DnDSimpleFocusPanel book = (DnDSimpleFocusPanel) widget;
                                                html = book.cloneHTML();
                                        }
-                                       if (html == null)
+                                       if(html == null)
                                                container.add(new Label("Drag ME"));
                                        else
                                                container.add(html);
+                                       return container;
                                }
                                return container;
                        }
@@ -304,64 +319,54 @@ public class GSS implements EntryPoint, ResizeHandler {
                        @Override
                        public void onSelection(SelectionEvent<Integer> event) {
                                int tabIndex = event.getSelectedItem();
-                               TreeItem treeItem = GSS.get().getFolders().getCurrent();
-                               PopupTree tree = GSS.get().getFolders().getPopupTree();
+//                             TreeItem treeItem = GSS.get().getFolders().getCurrent();
                                switch (tabIndex) {
                                        case 0:
+//                                             Files tab selected
                                                fileList.clearSelectedRows();
                                                fileList.updateCurrentlyShowingStats();
                                                break;
-                                               /**
-                                                * When the Groups tab is selected a new
-                                                */
                                        case 1:
+//                                             Groups tab selected
                                                groups.updateCurrentlyShowingStats();
-//                                             Add a new pair key - object in the History map.
-                                               setHistory("Groups", treeItem);
-//                                             Add a new browser history entry.
-                                       History.newItem("Groups");
+                                       updateHistory("Groups");
                                                break;
                                        case 2:
+//                                             Search tab selected
                                                searchResults.clearSelectedRows();
                                                searchResults.updateCurrentlyShowingStats();
-                                               setHistory("Search", treeItem);
-                                       History.newItem("Search");
+                                       updateHistory("Search");
                                                break;
                                }
                        }
                });
-               // If the application starts with no history token, redirect to a new "Files" state
+//             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
-                  History.addValueChangeHandler(new ValueChangeHandler<String>() {
-                             public void onValueChange(ValueChangeEvent<String> event) {
-                               String historyToken = event.getValue();
-                               TreeItem treeItem = GSS.get().getFolders().getCurrent();
-                               try {
-                                 if(historyToken.equals("Search"))
+//                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")){
-                                         setHistory("Files", treeItem);
-                                         History.newItem("Files");
-                                         inner.selectTab(0);
-                                 }
-                                 else{
-                                         PopupTree tree = GSS.get().getFolders().getPopupTree();
-                                         String newHistoryToken = URL.decode(historyToken);
-                                         System.out.println(map.keySet());
-                                         SelectionEvent.fire(tree, (TreeItem) getHistoryItem(newHistoryToken));
-                                 }
-                               } catch (IndexOutOfBoundsException e) {
-                                       inner.selectTab(0);
-                               }
-                             }
-                           });
-
+                                       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);
@@ -397,6 +402,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());
                        }
@@ -419,6 +425,7 @@ public class GSS implements EntryPoint, ResizeHandler {
                                if (announcement != null)
                                        DeferredCommand.addCommand(new Command() {
 
+                                               @Override
                                                public void execute() {
                                                        displayInformation(announcement);
                                                }
@@ -445,9 +452,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.
@@ -456,7 +460,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();
@@ -465,6 +469,7 @@ public class GSS implements EntryPoint, ResizeHandler {
 
                DeferredCommand.addCommand(new Command() {
 
+                       @Override
                        public void execute() {
                                fetchUser(username);
                        }
@@ -480,10 +485,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());
        }
 
@@ -793,21 +802,83 @@ public class GSS implements EntryPoint, ResizeHandler {
                webDAVPassword = Cookies.getCookie(cookie);
                Cookies.setCookie(cookie, "", null, domain, path, false);
        }
+
        /**
+        * Convert server date to local time according to browser timezone
+        * and format it according to localized pattern.
+        * Time is always formatted to 24hr format.
+        * NB: This assumes that server runs in UTC timezone. Otherwise
+        * we would need to adjust for server time offset as well.
         *
-        * @param key
-        * @return Object of the corresponding key in the History map
+        * @param date
+        * @return String
         */
-       public Object getHistoryItem(String key){
-               return map.get(key);
+       public static String formatLocalDateTime(Date date) {
+               Date convertedDate = new Date(date.getTime() - date.getTimezoneOffset());
+               final DateTimeFormat dateFormatter = DateTimeFormat.getShortDateFormat();
+               final DateTimeFormat timeFormatter = DateTimeFormat.getFormat("HH:mm");
+               String datePart = dateFormatter.format(convertedDate);
+               String timePart = timeFormatter.format(convertedDate);
+               return datePart + " " + timePart;
        }
+       
        /**
-        * Set a pair of key - object in the History (using a map)
+        * History support for folder navigation
+        * adds a new browser history entry
+        *
         * @param key
-        * @param obj
         */
+       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;
 
-       public void setHistory(String key, Object obj){
-               map.put(key, obj);
+       }
+
+       /**
+        * Reject illegal resource names, like '.' or '..' or slashes '/'.
+        */
+       static boolean isValidResourceName(String name) {
+               if (".".equals(name) || "..".equals(name) || name.contains("/"))
+                       return false;
+               return true;
+       }
+
+       public void putUserToMap(String _userName, String _userFullName){
+               userFullNameMap.put(_userName, _userFullName);
+       }
+
+       public String findUserFullName(String _userName){
+               return userFullNameMap.get(_userName);
+       }
+
+       public String getUserFullName(String _userName) {
+               if (GSS.get().findUserFullName(_userName) == null)
+                       //if there is no userFullName found then the map fills with the given _userName,
+                       //so userFullName = _userName
+                       GSS.get().putUserToMap(_userName, _userName);
+               else if(GSS.get().findUserFullName(_userName).indexOf('@') != -1){
+                       //if the userFullName = _userName the GetUserCommand updates the userFullName in the map
+                       GetUserCommand guc = new GetUserCommand(_userName);
+                       guc.execute();
+               }
+               return GSS.get().findUserFullName(_userName);
        }
 }