Partially implemented file upload. It only creates a 0 sized object
[pithos] / web_client / src / gr / grnet / pithos / web / client / StatusPanel.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
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.
16  *
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.
29  *
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.
34  */
35 package gr.grnet.pithos.web.client;
36
37 import com.google.gwt.core.client.Scheduler;
38 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
39 import gr.grnet.pithos.web.client.foldertree.AccountResource;
40 import gr.grnet.pithos.web.client.rest.GetCommand;
41 import gr.grnet.pithos.web.client.rest.GetRequest;
42 import gr.grnet.pithos.web.client.rest.RestException;
43 import gr.grnet.pithos.web.client.rest.resource.QuotaHolder;
44 import gr.grnet.pithos.web.client.rest.resource.UserResource;
45
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.i18n.client.DateTimeFormat;
48 import com.google.gwt.resources.client.ClientBundle;
49 import com.google.gwt.resources.client.ImageResource;
50 import com.google.gwt.user.client.ui.AbstractImagePrototype;
51 import com.google.gwt.user.client.ui.Composite;
52 import com.google.gwt.user.client.ui.HTML;
53 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54 import com.google.gwt.user.client.ui.HorizontalPanel;
55 import java.util.Date;
56
57 /**
58  * The panel that displays a status bar with quota information.
59  */
60 public class StatusPanel extends Composite {
61         public static final boolean DONE = false;
62         private HTML fileCountLabel = new HTML("");
63         private HTML fileSizeLabel = new HTML("");
64         private HTML quotaIcon = new HTML("");
65         private HTML quotaLabel = new HTML("");
66         private HTML lastLoginLabel = new HTML("");
67         private HTML currentLoginLabel = new HTML("");
68         private HTML currentlyShowingLabel = new HTML("");
69
70         /**
71          * An image bundle for this widget's images.
72          */
73         public interface Images extends ClientBundle {
74
75                 @Source("gr/grnet/pithos/resources/windowlist.png")
76                 ImageResource totalFiles();
77
78                 @Source("gr/grnet/pithos/resources/database.png")
79                 ImageResource totalSize();
80
81                 @Source("gr/grnet/pithos/resources/redled.png")
82                 ImageResource redSize();
83
84                 @Source("gr/grnet/pithos/resources/greenled.png")
85                 ImageResource greenSize();
86
87                 @Source("gr/grnet/pithos/resources/yellowled.png")
88                 ImageResource yellowSize();
89
90                 @Source("gr/grnet/pithos/resources/xclock.png")
91                 ImageResource lastLogin();              
92         }
93
94         private final Images images;
95
96         /**
97          * The constructor of the status panel.
98          *
99          * @param theImages the supplied images
100          */
101         public StatusPanel(Images theImages) {
102                 images = theImages;
103                 HorizontalPanel outer = new HorizontalPanel();
104                 outer.setWidth("100%");
105                 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
106
107                 HorizontalPanel left = new HorizontalPanel();
108                 left.setSpacing(8);
109                 HorizontalPanel middle = new HorizontalPanel();
110                 middle.setSpacing(8);
111                 HorizontalPanel right = new HorizontalPanel();
112                 right.setSpacing(8);
113                 outer.add(left);
114                 outer.add(middle);
115                 outer.add(right);
116                 left.add(new HTML("<b>Totals:</b> "));
117                 left.add(AbstractImagePrototype.create(images.totalFiles()).createImage());
118                 left.add(fileCountLabel);
119                 left.add(AbstractImagePrototype.create(images.totalSize()).createImage());
120                 left.add(fileSizeLabel);
121                 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
122                 left.add(quotaIcon);
123                 left.add(quotaLabel);
124                 middle.add(AbstractImagePrototype.create(images.lastLogin()).createImage());
125                 middle.add(new HTML("<b>Last login:</b> "));
126                 middle.add(lastLoginLabel);
127                 middle.add(new HTML("<b>\u0387 Current session login:</b> "));
128                 middle.add(currentLoginLabel);
129                 right.add(currentlyShowingLabel);
130                 outer.setStyleName("statusbar-inner");
131                 left.setStyleName("statusbar-inner");
132                 middle.setStyleName("statusbar-inner");
133                 right.setStyleName("statusbar-inner");
134                 outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);
135
136                 initWidget(outer);
137         }
138
139         /**
140          * Refresh the widget with the provided statistics.
141          */
142         public void displayStats(AccountResource account) {
143                 if (account.getNumberOfObjects() == 1)
144                         fileCountLabel.setHTML("1 object");
145                 else
146                         fileCountLabel.setHTML(account.getNumberOfObjects() + " objects");
147                 fileSizeLabel.setHTML(account.getFileSizeAsString() + " used");
148                 long pc = (long) ((double) account.getBytesRemaining()/(account.getBytesRemaining() + account.getBytesUsed()) + 0.5);
149                 if (pc < 10) {
150                         quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
151                         quotaLabel.setHTML(account.getQuotaLeftAsString() + " free");
152                 } else if(pc<20) {
153                         quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
154                         quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
155                 } else {
156                         quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
157                         quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
158                 }
159                 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
160         Date login = account.getLastLogin();
161                 lastLoginLabel.setHTML(login == null ? "" : formatter.format(login));
162
163         login = account.getCurrentLogin();
164                 currentLoginLabel.setHTML(login == null ? "" : formatter.format(login));
165         }
166
167         /**
168          * Requests updated quota information from the server and refreshes
169          * the display.
170          */
171     //TODO: This should not be done here
172         public void updateStats() {
173                 final GSS app = GSS.get();
174         String path = app.getApiPath() + app.getUsername();
175         GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, path) {
176             @Override
177             public void onSuccess(AccountResource result) {
178                 displayStats(result);
179             }
180
181             @Override
182             public void onError(Throwable t) {
183                 if(t instanceof RestException)
184                     app.displayError("Unable to fetch quota:" +
185                                 ((RestException)t).getHttpStatusText());
186                 else
187                     app.displayError("System error fetching quota:" +
188                                 t.getMessage());
189                 GWT.log("ERR", t);
190             }
191         };
192         getAccount.setHeader("X-Auth-Token", app.getToken());
193         Scheduler.get().scheduleDeferred(getAccount);
194         }
195
196         /**
197          * Displays the statistics for the current folder.
198          *
199          * @param text the statistics to display
200          */
201         public void updateCurrentlyShowing(String text) {
202                 if (text == null)
203                         currentlyShowingLabel.setText("");
204                 else
205                         currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);
206         }
207
208 }