Merge branch 'packaging' into debian
[pithos-web-client] / src / gr / grnet / pithos / web / client / foldertree / AccountResource.java
index 6c5e5f8..c437598 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or
  * without modification, are permitted provided that the following
@@ -44,6 +44,7 @@ import java.util.List;
 
 import com.google.gwt.http.client.Header;
 import com.google.gwt.http.client.Response;
+import com.google.gwt.http.client.URL;
 import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
 import com.google.gwt.i18n.client.NumberFormat;
@@ -64,7 +65,7 @@ public class AccountResource extends Resource {
 
     private long bytesUsed = 0;
 
-    private long bytesRemaining = 0;
+    private long quota = 0;
 
     private Date lastLogin = null;
 
@@ -76,12 +77,12 @@ public class AccountResource extends Resource {
 
     private List<Group> groups = new ArrayList<Group>();
 
-    public long getBytesRemaining() {
-        return bytesRemaining;
+    public long getQuota() {
+        return quota;
     }
 
-    public void setBytesRemaining(long bytesRemaining) {
-        this.bytesRemaining = bytesRemaining;
+    public void setQuota(long quota) {
+        this.quota = quota;
     }
 
     public long getBytesUsed() {
@@ -139,37 +140,41 @@ public class AccountResource extends Resource {
 
     public void populate(String owner, Response response) {
         DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
+        groups.clear();
         for (Header h : response.getHeaders()) {
-            String name = h.getName();
-            if (name.startsWith("X-Account-Group-")) {
-                String groupName = name.substring("X-Account-Group-".length()).trim().toLowerCase();
-                Group g = new Group(groupName);
-                String[] members = h.getValue().split(",");
-                for (String s : members)
-                    g.addMember(s.trim());
-                groups.add(g);
-            }
-            else if (name.equals("X-Account-Container-Count")) {
-                numberOfContainers = Long.valueOf(h.getValue());
-            }
-            else if (name.equals("X-Account-Object-Count")) {
-                numberOfObjects = Long.valueOf(h.getValue());
-            }
-            else if (name.equals("X-Account-Bytes-Used")) {
-                bytesUsed = Long.valueOf(h.getValue());
-            }
-            else if (name.equals("X-Account-Bytes-Remaining")) {
-                bytesRemaining = Long.valueOf(h.getValue());
-            }
-            else if (name.equals("X-Account-Last-Login")) {
-                lastLogin = df.parse(h.getValue());
-            }
-            else if (name.equals("Last-Modified")) {
-                lastModified = df.parse(h.getValue());
-            }
+               if (h != null) {
+                       String name = h.getName();
+                       if (name.startsWith("X-Account-Group-")) {
+                           String groupName = URL.decodePathSegment(name.substring("X-Account-Group-".length()));
+                           Group g = new Group(groupName);
+                           String[] members = h.getValue().split(",");
+                           for (String s : members)
+                               g.addMember(URL.decodePathSegment(s).trim());
+                           groups.add(g);
+                       }
+                       else if (name.equals("X-Account-Container-Count")) {
+                           numberOfContainers = Long.valueOf(h.getValue());
+                       }
+                       else if (name.equals("X-Account-Object-Count")) {
+                           numberOfObjects = Long.valueOf(h.getValue());
+                       }
+                       else if (name.equals("X-Account-Bytes-Used")) {
+                           bytesUsed = Long.valueOf(h.getValue());
+                       }
+                       else if (name.equals("X-Account-Policy-Quota")) {
+                           quota = Long.valueOf(h.getValue());
+                       }
+                       else if (name.equals("X-Account-Last-Login")) {
+                           lastLogin = df.parse(h.getValue());
+                       }
+                       else if (name.equals("Last-Modified")) {
+                           lastModified = df.parse(h.getValue());
+                       }
+               }
         }
 
         if (response.getText() != null && response.getText().length() > 0) {
+               containers.clear();
                JSONValue json = JSONParser.parseStrict(response.getText());
                JSONArray array = json.isArray();
                if (array != null) {
@@ -203,23 +208,22 @@ public class AccountResource extends Resource {
 
     public String getFileSizeAsString() {
         if (bytesUsed < 1024)
-            return String.valueOf(bytesUsed) + " B";
+            return String.valueOf(bytesUsed) + "B";
         else if (bytesUsed < 1024*1024)
-            return getSize(bytesUsed, 1024D) + " KB";
+            return getSize(bytesUsed, 1024D) + "KB";
         else if (bytesUsed < 1024*1024*1024)
-            return getSize(bytesUsed,(1024D*1024D)) + " MB";
-        return getSize(bytesUsed , (1024D*1024D*1024D)) + " GB";
+            return getSize(bytesUsed,(1024D*1024D)) + "MB";
+        return getSize(bytesUsed , (1024D*1024D*1024D)) + "GB";
     }
 
     public String getQuotaAsString() {
-       long quota = bytesUsed + bytesRemaining;
         if (quota < 1024)
-            return String.valueOf(quota) + " B";
+            return String.valueOf(quota) + "B";
         else if (quota < 1024 * 1024)
-            return getSize(quota, 1024D) + " KB";
+            return getSize(quota, 1024D) + "KB";
         else if (quota < 1024 * 1024 * 1024)
-            return getSize(quota,(1024D * 1024D)) + " MB";
-        return getSize(quota , (1024D * 1024D * 1024D)) + " GB";
+            return getSize(quota,(1024D * 1024D)) + "MB";
+        return getSize(quota , (1024D * 1024D * 1024D)) + "GB";
     }
 
     public List<Group> getGroups() {
@@ -257,7 +261,9 @@ public class AccountResource extends Resource {
        }
 
        public double getUsedPercentage() {
-               return 100.0 * bytesUsed / (bytesUsed + bytesRemaining);
+               if (quota == 0)
+                       return 0;
+               return ((double) bytesUsed) / quota;
        }
 
        public Folder getPithos() {