Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / Pithos.java @ efede616

History | View | Annotate | Download (44.3 kB)

1 a57faaf0 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client;
36 a57faaf0 Christos Stathis
37 bbdd34ef Christos Stathis
import gr.grnet.pithos.web.client.commands.UploadFileCommand;
38 6084aa02 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.AccountResource;
39 875a0179 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
40 7c818c14 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
41 6084aa02 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
42 7c818c14 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
43 602ebeb4 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
44 4baffab1 Christos Stathis
import gr.grnet.pithos.web.client.grouptree.Group;
45 ee9fbc37 Christos Stathis
import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
46 ee9fbc37 Christos Stathis
import gr.grnet.pithos.web.client.grouptree.GroupTreeViewModel;
47 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView;
48 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
49 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
50 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
51 602ebeb4 Christos Stathis
import gr.grnet.pithos.web.client.rest.DeleteRequest;
52 6084aa02 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
53 64f42c0b Christos Stathis
import gr.grnet.pithos.web.client.rest.HeadRequest;
54 d6be39da Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
55 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
56 a6592a15 Christos Stathis
import gr.grnet.pithos.web.client.tagtree.Tag;
57 7811b9d1 Christos Stathis
58 a57faaf0 Christos Stathis
import java.util.ArrayList;
59 a57faaf0 Christos Stathis
import java.util.HashMap;
60 7c818c14 Christos Stathis
import java.util.Iterator;
61 a57faaf0 Christos Stathis
import java.util.List;
62 7811b9d1 Christos Stathis
import java.util.Set;
63 a57faaf0 Christos Stathis
64 a57faaf0 Christos Stathis
import com.google.gwt.core.client.EntryPoint;
65 a57faaf0 Christos Stathis
import com.google.gwt.core.client.GWT;
66 7811b9d1 Christos Stathis
import com.google.gwt.core.client.Scheduler;
67 7811b9d1 Christos Stathis
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
68 7811b9d1 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
69 7811b9d1 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
70 a57faaf0 Christos Stathis
import com.google.gwt.event.logical.shared.ResizeEvent;
71 a57faaf0 Christos Stathis
import com.google.gwt.event.logical.shared.ResizeHandler;
72 7811b9d1 Christos Stathis
import com.google.gwt.http.client.Request;
73 7811b9d1 Christos Stathis
import com.google.gwt.http.client.RequestBuilder;
74 7811b9d1 Christos Stathis
import com.google.gwt.http.client.RequestCallback;
75 7811b9d1 Christos Stathis
import com.google.gwt.http.client.RequestException;
76 7811b9d1 Christos Stathis
import com.google.gwt.http.client.Response;
77 f5023f13 Christos Stathis
import com.google.gwt.http.client.URL;
78 f0105e9f Christos Stathis
import com.google.gwt.i18n.client.Dictionary;
79 29b2177c Christos Stathis
import com.google.gwt.i18n.client.NumberFormat;
80 7811b9d1 Christos Stathis
import com.google.gwt.json.client.JSONArray;
81 7811b9d1 Christos Stathis
import com.google.gwt.json.client.JSONObject;
82 7811b9d1 Christos Stathis
import com.google.gwt.json.client.JSONParser;
83 7811b9d1 Christos Stathis
import com.google.gwt.json.client.JSONString;
84 7811b9d1 Christos Stathis
import com.google.gwt.json.client.JSONValue;
85 a57faaf0 Christos Stathis
import com.google.gwt.resources.client.ImageResource;
86 9fbf5596 Christos Stathis
import com.google.gwt.resources.client.ImageResource.ImageOptions;
87 7811b9d1 Christos Stathis
import com.google.gwt.user.client.Command;
88 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Cookies;
89 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Event;
90 a57faaf0 Christos Stathis
import com.google.gwt.user.client.History;
91 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Window;
92 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
93 7811b9d1 Christos Stathis
import com.google.gwt.user.client.ui.Button;
94 0549e173 Christos Stathis
import com.google.gwt.user.client.ui.Composite;
95 7811b9d1 Christos Stathis
import com.google.gwt.user.client.ui.HTML;
96 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
97 7811b9d1 Christos Stathis
import com.google.gwt.user.client.ui.HasVerticalAlignment;
98 7811b9d1 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
99 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
100 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.RootPanel;
101 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
102 7811b9d1 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
103 7811b9d1 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
104 7811b9d1 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
105 6084aa02 Christos Stathis
106 a57faaf0 Christos Stathis
/**
107 a57faaf0 Christos Stathis
 * Entry point classes define <code>onModuleLoad()</code>.
108 a57faaf0 Christos Stathis
 */
109 749068ba Christos Stathis
public class Pithos implements EntryPoint, ResizeHandler {
110 a57faaf0 Christos Stathis
111 105b4e49 Christos Stathis
        public static final String HOME_CONTAINER = "pithos";
112 7b28ae07 Christos Stathis
113 7b28ae07 Christos Stathis
        public static final String TRASH_CONTAINER = "trash";
114 2dfdda3f Christos Stathis
115 2dfdda3f Christos Stathis
        public static final Configuration config = GWT.create(Configuration.class);
116 105b4e49 Christos Stathis
        
117 a57faaf0 Christos Stathis
        /**
118 a57faaf0 Christos Stathis
         * Instantiate an application-level image bundle. This object will provide
119 a57faaf0 Christos Stathis
         * programmatic access to all the images needed by widgets.
120 a57faaf0 Christos Stathis
         */
121 a9e497d4 Christos Stathis
        static Images images = (Images) GWT.create(Images.class);
122 a57faaf0 Christos Stathis
123 6084aa02 Christos Stathis
    public String getUsername() {
124 6084aa02 Christos Stathis
        return username;
125 6084aa02 Christos Stathis
    }
126 a57faaf0 Christos Stathis
127 8e61880b Christos Stathis
    public void setAccount(AccountResource acct) {
128 8e61880b Christos Stathis
        account = acct;
129 8e61880b Christos Stathis
    }
130 8e61880b Christos Stathis
131 8e61880b Christos Stathis
    public AccountResource getAccount() {
132 8e61880b Christos Stathis
        return account;
133 8e61880b Christos Stathis
    }
134 8e61880b Christos Stathis
135 16e8478a Christos Stathis
    public void updateFolder(Folder f, boolean showfiles, Command callback) {
136 16e8478a Christos Stathis
        folderTreeView.updateFolder(f, showfiles, callback);
137 b651e67f Christos Stathis
    }
138 b651e67f Christos Stathis
139 4baffab1 Christos Stathis
    public void updateGroupNode(Group group) {
140 4baffab1 Christos Stathis
        groupTreeView.updateGroupNode(group);
141 4baffab1 Christos Stathis
    }
142 4baffab1 Christos Stathis
143 3601b114 Christos Stathis
    public void updateMySharedRoot() {
144 3601b114 Christos Stathis
            mysharedTreeView.updateRoot();
145 3601b114 Christos Stathis
    }
146 3601b114 Christos Stathis
    
147 105b4e49 Christos Stathis
    public void updateSharedFolder(Folder f, boolean showfiles) {
148 105b4e49 Christos Stathis
            mysharedTreeView.updateFolder(f, showfiles);
149 105b4e49 Christos Stathis
    }
150 105b4e49 Christos Stathis
    
151 6d8e0f58 Christos Stathis
    public void updateOtherSharedFolder(Folder f, boolean showfiles) {
152 6d8e0f58 Christos Stathis
            otherSharedTreeView.updateFolder(f, showfiles);
153 6d8e0f58 Christos Stathis
    }
154 6d8e0f58 Christos Stathis
155 105b4e49 Christos Stathis
    public MysharedTreeView getMySharedTreeView() {
156 105b4e49 Christos Stathis
        return mysharedTreeView;
157 105b4e49 Christos Stathis
    }
158 105b4e49 Christos Stathis
159 6084aa02 Christos Stathis
    /**
160 a57faaf0 Christos Stathis
         * An aggregate image bundle that pulls together all the images for this
161 a57faaf0 Christos Stathis
         * application into a single bundle.
162 a57faaf0 Christos Stathis
         */
163 a9e497d4 Christos Stathis
        public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
164 a57faaf0 Christos Stathis
165 a57faaf0 Christos Stathis
                @Source("gr/grnet/pithos/resources/document.png")
166 a57faaf0 Christos Stathis
                ImageResource folders();
167 a57faaf0 Christos Stathis
168 a9e497d4 Christos Stathis
                @Source("gr/grnet/pithos/resources/advancedsettings.png")
169 9fbf5596 Christos Stathis
                @ImageOptions(width=32, height=32)
170 a9e497d4 Christos Stathis
                ImageResource tools();
171 a57faaf0 Christos Stathis
        }
172 a57faaf0 Christos Stathis
173 3f8622d4 Christos Stathis
        private Throwable error;
174 3f8622d4 Christos Stathis
        
175 a57faaf0 Christos Stathis
        /**
176 a57faaf0 Christos Stathis
         * The Application Clipboard implementation;
177 a57faaf0 Christos Stathis
         */
178 a57faaf0 Christos Stathis
        private Clipboard clipboard = new Clipboard();
179 a57faaf0 Christos Stathis
180 a57faaf0 Christos Stathis
        /**
181 a57faaf0 Christos Stathis
         * The top panel that contains the menu bar.
182 a57faaf0 Christos Stathis
         */
183 a57faaf0 Christos Stathis
        private TopPanel topPanel;
184 a57faaf0 Christos Stathis
185 a57faaf0 Christos Stathis
        /**
186 a57faaf0 Christos Stathis
         * The panel that contains the various system messages.
187 a57faaf0 Christos Stathis
         */
188 3f8622d4 Christos Stathis
        private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
189 a57faaf0 Christos Stathis
190 a57faaf0 Christos Stathis
        /**
191 a57faaf0 Christos Stathis
         * The bottom panel that contains the status bar.
192 a57faaf0 Christos Stathis
         */
193 8e61880b Christos Stathis
        private StatusPanel statusPanel = null;
194 a57faaf0 Christos Stathis
195 a57faaf0 Christos Stathis
        /**
196 a57faaf0 Christos Stathis
         * The file list widget.
197 a57faaf0 Christos Stathis
         */
198 a57faaf0 Christos Stathis
        private FileList fileList;
199 a57faaf0 Christos Stathis
200 a57faaf0 Christos Stathis
        /**
201 a57faaf0 Christos Stathis
         * The tab panel that occupies the right side of the screen.
202 a57faaf0 Christos Stathis
         */
203 bbdd34ef Christos Stathis
        private VerticalPanel inner = new VerticalPanel();
204 a57faaf0 Christos Stathis
205 63366925 Christos Stathis
206 a57faaf0 Christos Stathis
        /**
207 a57faaf0 Christos Stathis
         * The split panel that will contain the left and right panels.
208 a57faaf0 Christos Stathis
         */
209 a57faaf0 Christos Stathis
        private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
210 a57faaf0 Christos Stathis
211 a57faaf0 Christos Stathis
        /**
212 a57faaf0 Christos Stathis
         * The currently selected item in the application, for use by the Edit menu
213 a57faaf0 Christos Stathis
         * commands. Potential types are Folder, File, User and Group.
214 a57faaf0 Christos Stathis
         */
215 a57faaf0 Christos Stathis
        private Object currentSelection;
216 a57faaf0 Christos Stathis
217 a57faaf0 Christos Stathis
        public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
218 a57faaf0 Christos Stathis
219 6084aa02 Christos Stathis
    private String username = null;
220 6084aa02 Christos Stathis
221 6084aa02 Christos Stathis
    /**
222 6084aa02 Christos Stathis
     * The authentication token of the current user.
223 6084aa02 Christos Stathis
     */
224 6084aa02 Christos Stathis
    private String token;
225 6084aa02 Christos Stathis
226 7c54d867 Christos Stathis
    VerticalPanel trees;
227 7c54d867 Christos Stathis
    
228 f1b24b82 Christos Stathis
    SingleSelectionModel<Folder> folderTreeSelectionModel;
229 f1b24b82 Christos Stathis
    FolderTreeViewModel folderTreeViewModel;
230 f1b24b82 Christos Stathis
    FolderTreeView folderTreeView;
231 6084aa02 Christos Stathis
232 f1b24b82 Christos Stathis
    SingleSelectionModel<Folder> mysharedTreeSelectionModel;
233 7c54d867 Christos Stathis
    MysharedTreeViewModel mysharedTreeViewModel;
234 7c54d867 Christos Stathis
    MysharedTreeView mysharedTreeView = null;;
235 105b4e49 Christos Stathis
236 7811b9d1 Christos Stathis
    protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
237 7c54d867 Christos Stathis
    OtherSharedTreeViewModel otherSharedTreeViewModel;
238 7c54d867 Christos Stathis
    OtherSharedTreeView otherSharedTreeView = null;
239 a6592a15 Christos Stathis
240 abd8738f Christos Stathis
    GroupTreeViewModel groupTreeViewModel;
241 ee9fbc37 Christos Stathis
    private GroupTreeView groupTreeView;
242 ee9fbc37 Christos Stathis
243 c4b7a6bb Christos Stathis
    TreeView selectedTree;
244 7811b9d1 Christos Stathis
    protected AccountResource account;
245 16e8478a Christos Stathis
    
246 abd8738f Christos Stathis
    Folder trash;
247 0549e173 Christos Stathis
    
248 0549e173 Christos Stathis
    List<Composite> treeViews = new ArrayList<Composite>();
249 8e61880b Christos Stathis
250 7c54d867 Christos Stathis
    @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
251 3ebb88f4 Christos Stathis
    
252 3ebb88f4 Christos Stathis
    Button upload;
253 bf664298 Christos Stathis
    
254 bf664298 Christos Stathis
    private HTML usedBytes;
255 bf664298 Christos Stathis
    
256 bf664298 Christos Stathis
    private HTML totalBytes;
257 bf664298 Christos Stathis
    
258 bf664298 Christos Stathis
    private HTML usedPercent;
259 a19f758c Christos Stathis
    
260 a19f758c Christos Stathis
    private HTML numOfFiles;
261 b162dfaa Christos Stathis
    
262 b75210b5 Christos Stathis
    private Toolbar toolbar;
263 b75210b5 Christos Stathis
    
264 1a0db4f4 Christos Stathis
    private FileUploadDialog fileUploadDialog;
265 1a0db4f4 Christos Stathis
    
266 a57faaf0 Christos Stathis
        @Override
267 a57faaf0 Christos Stathis
        public void onModuleLoad() {
268 6084aa02 Christos Stathis
                if (parseUserCredentials())
269 6084aa02 Christos Stathis
            initialize();
270 6084aa02 Christos Stathis
        }
271 a57faaf0 Christos Stathis
272 6084aa02 Christos Stathis
    private void initialize() {
273 a7b6e464 Christos Stathis
            boolean bareContent = Window.Location.getParameter("noframe") != null;
274 a7b6e464 Christos Stathis
            String contentWidth = bareContent ? "100%" : "75%";
275 bbdd34ef Christos Stathis
276 a7b6e464 Christos Stathis
            VerticalPanel outer = new VerticalPanel();
277 a7b6e464 Christos Stathis
        outer.setWidth("100%");
278 a7b6e464 Christos Stathis
            if (!bareContent) {
279 a7b6e464 Christos Stathis
                    outer.addStyleName("pithos-outer");
280 a7b6e464 Christos Stathis
            }
281 a7b6e464 Christos Stathis
282 a7b6e464 Christos Stathis
        if (!bareContent) {
283 a7b6e464 Christos Stathis
                topPanel = new TopPanel(this, Pithos.images);
284 a7b6e464 Christos Stathis
                topPanel.setWidth("100%");
285 a7b6e464 Christos Stathis
                outer.add(topPanel);
286 a7b6e464 Christos Stathis
                outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
287 a7b6e464 Christos Stathis
        }
288 a76065b6 Christos Stathis
        
289 6cf258b1 Christos Stathis
        messagePanel.setVisible(false);
290 6cf258b1 Christos Stathis
        outer.add(messagePanel);
291 6cf258b1 Christos Stathis
        outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
292 6cf258b1 Christos Stathis
        outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
293 6cf258b1 Christos Stathis
294 a7b6e464 Christos Stathis
        HorizontalPanel header = new HorizontalPanel();
295 a7b6e464 Christos Stathis
        header.addStyleName("pithos-header");
296 2d91458a Christos Stathis
        header.setWidth(contentWidth);
297 0549e173 Christos Stathis
        if (bareContent)
298 0549e173 Christos Stathis
                header.addStyleName("pithos-header-noframe");
299 1a0db4f4 Christos Stathis
        upload = new Button("Upload", new ClickHandler() {
300 a7b6e464 Christos Stathis
            @Override
301 ebead1b5 Christos Stathis
            public void onClick(ClickEvent event) {
302 eb2ae14b Christos Stathis
                    if (getSelection() != null)
303 eb2ae14b Christos Stathis
                            new UploadFileCommand(Pithos.this, null, getSelection()).execute();
304 a7b6e464 Christos Stathis
            }
305 a7b6e464 Christos Stathis
        });
306 a7b6e464 Christos Stathis
        upload.addStyleName("pithos-uploadButton");
307 140f0f6d Christos Stathis
        header.add(upload);
308 140f0f6d Christos Stathis
        header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
309 140f0f6d Christos Stathis
        header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
310 6084aa02 Christos Stathis
311 b75210b5 Christos Stathis
        toolbar = new Toolbar(this);
312 bd187dfb Christos Stathis
        header.add(toolbar);
313 bd187dfb Christos Stathis
        header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
314 bd187dfb Christos Stathis
        header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
315 bd187dfb Christos Stathis
        
316 a19f758c Christos Stathis
        HorizontalPanel folderStatistics = new HorizontalPanel();
317 bbdd34ef Christos Stathis
        folderStatistics.addStyleName("pithos-folderStatistics");
318 a19f758c Christos Stathis
        numOfFiles = new HTML();
319 a19f758c Christos Stathis
        folderStatistics.add(numOfFiles);
320 a7b6e464 Christos Stathis
        folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
321 a19f758c Christos Stathis
        HTML numOfFilesLabel = new HTML("&nbsp;Files");
322 a19f758c Christos Stathis
        folderStatistics.add(numOfFilesLabel);
323 a7b6e464 Christos Stathis
        folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
324 140f0f6d Christos Stathis
        header.add(folderStatistics);
325 140f0f6d Christos Stathis
        header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
326 140f0f6d Christos Stathis
        header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
327 9fbf5596 Christos Stathis
        header.setCellWidth(folderStatistics, "40px");
328 a7b6e464 Christos Stathis
        outer.add(header);
329 a7b6e464 Christos Stathis
        outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
330 a7b6e464 Christos Stathis
        // Inner contains the various lists.nner
331 a7b6e464 Christos Stathis
        inner.sinkEvents(Event.ONCONTEXTMENU);
332 a7b6e464 Christos Stathis
        inner.setWidth("100%");
333 6084aa02 Christos Stathis
334 875a0179 Christos Stathis
        folderTreeSelectionModel = new SingleSelectionModel<Folder>();
335 875a0179 Christos Stathis
        folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
336 875a0179 Christos Stathis
            @Override
337 ebead1b5 Christos Stathis
            public void onSelectionChange(SelectionChangeEvent event) {
338 a845b044 Christos Stathis
                if (folderTreeSelectionModel.getSelectedObject() != null) {
339 f1b24b82 Christos Stathis
                    deselectOthers(folderTreeView, folderTreeSelectionModel);
340 3ebb88f4 Christos Stathis
                    applyPermissions(folderTreeSelectionModel.getSelectedObject());
341 a845b044 Christos Stathis
                    Folder f = folderTreeSelectionModel.getSelectedObject();
342 dddb75e2 Christos Stathis
                            updateFolder(f, true, new Command() {
343 dddb75e2 Christos Stathis
                                    
344 dddb75e2 Christos Stathis
                                    @Override
345 dddb75e2 Christos Stathis
                                    public void execute() {
346 dddb75e2 Christos Stathis
                                            updateStatistics();
347 dddb75e2 Christos Stathis
                                    }
348 dddb75e2 Christos Stathis
                            });
349 a845b044 Christos Stathis
                }
350 b75210b5 Christos Stathis
                showRelevantToolbarButtons();
351 875a0179 Christos Stathis
            }
352 875a0179 Christos Stathis
        });
353 105b4e49 Christos Stathis
        selectionModels.add(folderTreeSelectionModel);
354 875a0179 Christos Stathis
355 a7c43f26 Christos Stathis
        folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
356 875a0179 Christos Stathis
        folderTreeView = new FolderTreeView(folderTreeViewModel);
357 0549e173 Christos Stathis
        treeViews.add(folderTreeView);
358 0549e173 Christos Stathis
        
359 a7c43f26 Christos Stathis
        fileList = new FileList(this, images, folderTreeView);
360 bbdd34ef Christos Stathis
        inner.add(fileList);
361 875a0179 Christos Stathis
362 4baffab1 Christos Stathis
        groupTreeViewModel = new GroupTreeViewModel(this);
363 ee9fbc37 Christos Stathis
        groupTreeView = new GroupTreeView(groupTreeViewModel);
364 0549e173 Christos Stathis
        treeViews.add(groupTreeView);
365 0549e173 Christos Stathis
        
366 7c54d867 Christos Stathis
        trees = new VerticalPanel();
367 a76065b6 Christos Stathis
        trees.setWidth("100%");
368 bbdd34ef Christos Stathis
369 bbdd34ef Christos Stathis
        
370 bbdd34ef Christos Stathis
        HorizontalPanel treeHeader = new HorizontalPanel();
371 bbdd34ef Christos Stathis
        treeHeader.addStyleName("pithos-treeHeader");
372 a76065b6 Christos Stathis
        treeHeader.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
373 a7b6e464 Christos Stathis
        treeHeader.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
374 bf664298 Christos Stathis
        HorizontalPanel statistics = new HorizontalPanel();
375 a7b6e464 Christos Stathis
        statistics.addStyleName("pithos-statistics");
376 0f797b1b Christos Stathis
        statistics.add(new HTML("Used:&nbsp;"));
377 bf664298 Christos Stathis
        usedBytes = new HTML();
378 bf664298 Christos Stathis
        statistics.add(usedBytes);
379 bf664298 Christos Stathis
        statistics.add(new HTML("&nbsp;of&nbsp;"));
380 bf664298 Christos Stathis
        totalBytes = new HTML();
381 bf664298 Christos Stathis
        statistics.add(totalBytes);
382 bf664298 Christos Stathis
        statistics.add(new HTML("&nbsp;("));
383 bf664298 Christos Stathis
        usedPercent = new HTML();
384 bf664298 Christos Stathis
        statistics.add(usedPercent);
385 29b2177c Christos Stathis
        statistics.add(new HTML(")"));
386 bf664298 Christos Stathis
        treeHeader.add(statistics);
387 29b2177c Christos Stathis
        treeHeader.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_LEFT);
388 bbdd34ef Christos Stathis
        trees.add(treeHeader);
389 bbdd34ef Christos Stathis
390 a6592a15 Christos Stathis
        trees.add(folderTreeView);
391 ee9fbc37 Christos Stathis
        trees.add(groupTreeView);
392 6084aa02 Christos Stathis
        // Add the left and right panels to the split panel.
393 a6592a15 Christos Stathis
        splitPanel.setLeftWidget(trees);
394 6084aa02 Christos Stathis
        splitPanel.setRightWidget(inner);
395 a76065b6 Christos Stathis
        splitPanel.setSplitPosition("35%");
396 6084aa02 Christos Stathis
        splitPanel.setSize("100%", "100%");
397 6084aa02 Christos Stathis
        splitPanel.addStyleName("pithos-splitPanel");
398 a7b6e464 Christos Stathis
        splitPanel.setWidth(contentWidth);
399 6084aa02 Christos Stathis
        outer.add(splitPanel);
400 a76065b6 Christos Stathis
        outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
401 bbdd34ef Christos Stathis
402 a7b6e464 Christos Stathis
        if (!bareContent) {
403 a7b6e464 Christos Stathis
                statusPanel = new StatusPanel();
404 a7b6e464 Christos Stathis
                statusPanel.setWidth("100%");
405 a7b6e464 Christos Stathis
                outer.add(statusPanel);
406 a7b6e464 Christos Stathis
                outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
407 a7b6e464 Christos Stathis
        }
408 0549e173 Christos Stathis
        else
409 0549e173 Christos Stathis
                splitPanel.addStyleName("pithos-splitPanel-noframe");
410 6084aa02 Christos Stathis
411 6084aa02 Christos Stathis
        // Hook the window resize event, so that we can adjust the UI.
412 6084aa02 Christos Stathis
        Window.addResizeHandler(this);
413 6084aa02 Christos Stathis
        // Clear out the window's built-in margin, because we want to take
414 6084aa02 Christos Stathis
        // advantage of the entire client area.
415 6084aa02 Christos Stathis
        Window.setMargin("0px");
416 6084aa02 Christos Stathis
        // Finally, add the outer panel to the RootPanel, so that it will be
417 6084aa02 Christos Stathis
        // displayed.
418 6084aa02 Christos Stathis
        RootPanel.get().add(outer);
419 6084aa02 Christos Stathis
        // Call the window resized handler to get the initial sizes setup. Doing
420 6084aa02 Christos Stathis
        // this in a deferred command causes it to occur after all widgets'
421 6084aa02 Christos Stathis
        // sizes have been computed by the browser.
422 7c818c14 Christos Stathis
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
423 6084aa02 Christos Stathis
424 6084aa02 Christos Stathis
            @Override
425 6084aa02 Christos Stathis
            public void execute() {
426 6084aa02 Christos Stathis
                onWindowResized(Window.getClientHeight());
427 6084aa02 Christos Stathis
            }
428 6084aa02 Christos Stathis
        });
429 7c818c14 Christos Stathis
430 7c818c14 Christos Stathis
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
431 7c818c14 Christos Stathis
            @Override
432 7c818c14 Christos Stathis
            public void execute() {
433 64f42c0b Christos Stathis
                fetchAccount(new Command() {
434 64f42c0b Christos Stathis
                                        
435 64f42c0b Christos Stathis
                                        @Override
436 64f42c0b Christos Stathis
                                        public void execute() {
437 64f42c0b Christos Stathis
                                if (!account.hasHomeContainer())
438 64f42c0b Christos Stathis
                                    createHomeContainer(account, this);
439 64f42c0b Christos Stathis
                                else if (!account.hasTrashContainer())
440 64f42c0b Christos Stathis
                                        createTrashContainer(this);
441 64f42c0b Christos Stathis
                                else {
442 64f42c0b Christos Stathis
                                        for (Folder f : account.getContainers())
443 64f42c0b Christos Stathis
                                                if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
444 64f42c0b Christos Stathis
                                                        trash = f;
445 64f42c0b Christos Stathis
                                                        break;
446 64f42c0b Christos Stathis
                                                }
447 5fab761b Christos Stathis
                                    folderTreeViewModel.initialize(account, new Command() {
448 5fab761b Christos Stathis
                                                                
449 5fab761b Christos Stathis
                                                                @Override
450 5fab761b Christos Stathis
                                                                public void execute() {
451 5fab761b Christos Stathis
                                                    createMySharedTree();
452 5fab761b Christos Stathis
                                                                }
453 5fab761b Christos Stathis
                                                        });
454 64f42c0b Christos Stathis
                                    groupTreeViewModel.initialize();
455 64f42c0b Christos Stathis
                                    showStatistics();
456 64f42c0b Christos Stathis
                                }
457 64f42c0b Christos Stathis
                                        }
458 64f42c0b Christos Stathis
                                });
459 7c818c14 Christos Stathis
            }
460 7c818c14 Christos Stathis
        });
461 140f0f6d Christos Stathis
        
462 140f0f6d Christos Stathis
//        Scheduler.get().scheduleDeferred(new Command() {
463 140f0f6d Christos Stathis
//                        
464 140f0f6d Christos Stathis
//                        @Override
465 140f0f6d Christos Stathis
//                        public void execute() {
466 140f0f6d Christos Stathis
//                                displayError("lalala");
467 140f0f6d Christos Stathis
//                                
468 140f0f6d Christos Stathis
//                        }
469 140f0f6d Christos Stathis
//                });
470 6084aa02 Christos Stathis
    }
471 a57faaf0 Christos Stathis
472 3ebb88f4 Christos Stathis
    public void applyPermissions(Folder f) {
473 3ebb88f4 Christos Stathis
            if (f != null) {
474 3ebb88f4 Christos Stathis
                    if (f.isInTrash())
475 3ebb88f4 Christos Stathis
                            upload.setEnabled(false);
476 3ebb88f4 Christos Stathis
                    else {
477 3ebb88f4 Christos Stathis
                            Boolean[] perms = f.getPermissions().get(username);
478 3ebb88f4 Christos Stathis
                            if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
479 3ebb88f4 Christos Stathis
                                    upload.setEnabled(true);
480 3ebb88f4 Christos Stathis
                            }
481 3ebb88f4 Christos Stathis
                            else
482 3ebb88f4 Christos Stathis
                                    upload.setEnabled(false);
483 3ebb88f4 Christos Stathis
                    }
484 3ebb88f4 Christos Stathis
            }
485 3ebb88f4 Christos Stathis
            else
486 3ebb88f4 Christos Stathis
                    upload.setEnabled(false);
487 3ebb88f4 Christos Stathis
        }
488 3ebb88f4 Christos Stathis
489 3ebb88f4 Christos Stathis
        @SuppressWarnings({ "rawtypes", "unchecked" })
490 f1b24b82 Christos Stathis
        public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
491 f1b24b82 Christos Stathis
            selectedTree = _selectedTree;
492 0549e173 Christos Stathis
            
493 0549e173 Christos Stathis
            for (Composite c : treeViews)
494 0549e173 Christos Stathis
                    if (c.equals(selectedTree))
495 0549e173 Christos Stathis
                            c.addStyleName("cellTreeWidget-selectedTree");
496 0549e173 Christos Stathis
                    else
497 0549e173 Christos Stathis
                            c.removeStyleName("cellTreeWidget-selectedTree");
498 0549e173 Christos Stathis
            
499 105b4e49 Christos Stathis
        for (SingleSelectionModel s : selectionModels)
500 105b4e49 Christos Stathis
            if (!s.equals(model))
501 105b4e49 Christos Stathis
                s.setSelected(s.getSelectedObject(), false);
502 105b4e49 Christos Stathis
    }
503 105b4e49 Christos Stathis
504 dddb75e2 Christos Stathis
    public void showFiles(final Folder f) {
505 07a1b5fe Christos Stathis
        Set<File> files = f.getFiles();
506 a845b044 Christos Stathis
        showFiles(files);
507 a845b044 Christos Stathis
    }
508 a845b044 Christos Stathis
509 a845b044 Christos Stathis
    public void showFiles(Set<File> files) {
510 a845b044 Christos Stathis
        fileList.setFiles(new ArrayList<File>(files));
511 07a1b5fe Christos Stathis
    }
512 07a1b5fe Christos Stathis
513 875a0179 Christos Stathis
    /**
514 a57faaf0 Christos Stathis
         * Parse and store the user credentials to the appropriate fields.
515 a57faaf0 Christos Stathis
         */
516 6084aa02 Christos Stathis
        private boolean parseUserCredentials() {
517 36dc8e18 Christos Stathis
        username = Window.Location.getParameter("user");
518 36dc8e18 Christos Stathis
        token = Window.Location.getParameter("token");
519 36dc8e18 Christos Stathis
        Configuration conf = (Configuration) GWT.create(Configuration.class);
520 ff057ec1 Christos Stathis
                Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
521 36dc8e18 Christos Stathis
        if (username == null || username.length() == 0 || token == null || token.length() == 0) {
522 ff057ec1 Christos Stathis
            String cookie = otherProperties.get("authCookie");
523 36dc8e18 Christos Stathis
            String auth = Cookies.getCookie(cookie);
524 36dc8e18 Christos Stathis
            if (auth == null) {
525 6084aa02 Christos Stathis
                authenticateUser();
526 6084aa02 Christos Stathis
                return false;
527 6084aa02 Christos Stathis
            }
528 4645e885 Christos Stathis
            if (auth.startsWith("\""))
529 4645e885 Christos Stathis
                    auth = auth.substring(1);
530 4645e885 Christos Stathis
            if (auth.endsWith("\""))
531 4645e885 Christos Stathis
                    auth = auth.substring(0, auth.length() - 1);
532 7811b9d1 Christos Stathis
                        String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
533 7811b9d1 Christos Stathis
                        if (authSplit.length != 2) {
534 7811b9d1 Christos Stathis
                            authenticateUser();
535 7811b9d1 Christos Stathis
                            return false;
536 7811b9d1 Christos Stathis
                        }
537 7811b9d1 Christos Stathis
                        username = authSplit[0];
538 7811b9d1 Christos Stathis
                        token = authSplit[1];
539 36dc8e18 Christos Stathis
        }
540 4d869bf1 Christos Stathis
        else
541 4d869bf1 Christos Stathis
                Cookies.setCookie(otherProperties.get("authCookie"), username + conf.cookieSeparator() + token, null, "", "/", false);
542 4d869bf1 Christos Stathis
543 4d869bf1 Christos Stathis
        String gotoUrl = Window.Location.getParameter("goto");
544 4d869bf1 Christos Stathis
                if (gotoUrl != null && gotoUrl.length() > 0) {
545 4d869bf1 Christos Stathis
                        Window.Location.assign(gotoUrl + "?X-Auth-Token=" + token);
546 4d869bf1 Christos Stathis
                        return false;
547 4d869bf1 Christos Stathis
                }
548 7811b9d1 Christos Stathis
                return true;
549 36dc8e18 Christos Stathis
    }
550 a57faaf0 Christos Stathis
551 6084aa02 Christos Stathis
    /**
552 a57faaf0 Christos Stathis
         * Redirect the user to the login page for authentication.
553 a57faaf0 Christos Stathis
         */
554 a57faaf0 Christos Stathis
        protected void authenticateUser() {
555 f0105e9f Christos Stathis
                Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
556 4d869bf1 Christos Stathis
        Window.Location.assign(otherProperties.get("loginUrl") + Window.Location.getHref());
557 a57faaf0 Christos Stathis
        }
558 a57faaf0 Christos Stathis
559 64f42c0b Christos Stathis
        protected void fetchAccount(final Command callback) {
560 b51c628b Christos Stathis
        String path = "?format=json";
561 7c818c14 Christos Stathis
562 b51c628b Christos Stathis
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
563 7c818c14 Christos Stathis
            @Override
564 7811b9d1 Christos Stathis
            public void onSuccess(AccountResource _result) {
565 7811b9d1 Christos Stathis
                account = _result;
566 64f42c0b Christos Stathis
                if (callback != null)
567 64f42c0b Christos Stathis
                        callback.execute();
568 7c818c14 Christos Stathis
            }
569 7c818c14 Christos Stathis
570 7c818c14 Christos Stathis
            @Override
571 7c818c14 Christos Stathis
            public void onError(Throwable t) {
572 7c818c14 Christos Stathis
                GWT.log("Error getting account", t);
573 3f8622d4 Christos Stathis
                                setError(t);
574 7c818c14 Christos Stathis
                if (t instanceof RestException)
575 a7c43f26 Christos Stathis
                    displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
576 7c818c14 Christos Stathis
                else
577 a7c43f26 Christos Stathis
                    displayError("System error fetching user data: " + t.getMessage());
578 7c818c14 Christos Stathis
            }
579 9539e23d Christos Stathis
580 9539e23d Christos Stathis
                        @Override
581 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
582 9539e23d Christos Stathis
                                sessionExpired();
583 9539e23d Christos Stathis
                        }
584 7c818c14 Christos Stathis
        };
585 875a0179 Christos Stathis
        getAccount.setHeader("X-Auth-Token", token);
586 7c818c14 Christos Stathis
        Scheduler.get().scheduleDeferred(getAccount);
587 7c818c14 Christos Stathis
    }
588 7c818c14 Christos Stathis
589 64f42c0b Christos Stathis
    public void updateStatistics() {
590 64f42c0b Christos Stathis
            HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
591 64f42c0b Christos Stathis
592 64f42c0b Christos Stathis
                        @Override
593 ebead1b5 Christos Stathis
                        public void onSuccess(AccountResource _result) {
594 64f42c0b Christos Stathis
                                showStatistics();
595 64f42c0b Christos Stathis
                        }
596 64f42c0b Christos Stathis
597 64f42c0b Christos Stathis
                        @Override
598 64f42c0b Christos Stathis
                        public void onError(Throwable t) {
599 64f42c0b Christos Stathis
                GWT.log("Error getting account", t);
600 3f8622d4 Christos Stathis
                                setError(t);
601 64f42c0b Christos Stathis
                if (t instanceof RestException)
602 64f42c0b Christos Stathis
                    displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
603 64f42c0b Christos Stathis
                else
604 64f42c0b Christos Stathis
                    displayError("System error fetching user data: " + t.getMessage());
605 64f42c0b Christos Stathis
                        }
606 9539e23d Christos Stathis
607 9539e23d Christos Stathis
                        @Override
608 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
609 9539e23d Christos Stathis
                                sessionExpired();
610 9539e23d Christos Stathis
                        }
611 64f42c0b Christos Stathis
                };
612 64f42c0b Christos Stathis
                headAccount.setHeader("X-Auth-Token", token);
613 64f42c0b Christos Stathis
                Scheduler.get().scheduleDeferred(headAccount);
614 64f42c0b Christos Stathis
        }
615 64f42c0b Christos Stathis
616 64f42c0b Christos Stathis
        protected void showStatistics() {
617 bf664298 Christos Stathis
            usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
618 bf664298 Christos Stathis
            totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
619 29b2177c Christos Stathis
            NumberFormat nf = NumberFormat.getPercentFormat();
620 29b2177c Christos Stathis
            usedPercent.setHTML(nf.format(account.getUsedPercentage()));
621 bf664298 Christos Stathis
        }
622 bf664298 Christos Stathis
623 abd8738f Christos Stathis
        protected void createHomeContainer(final AccountResource _account, final Command callback) {
624 7b28ae07 Christos Stathis
        String path = "/" + Pithos.HOME_CONTAINER;
625 b51c628b Christos Stathis
        PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
626 b8822fe7 Christos Stathis
            @Override
627 ebead1b5 Christos Stathis
            public void onSuccess(Resource result) {
628 abd8738f Christos Stathis
                    if (!_account.hasTrashContainer())
629 64f42c0b Christos Stathis
                            createTrashContainer(callback);
630 7b28ae07 Christos Stathis
                    else
631 64f42c0b Christos Stathis
                            fetchAccount(callback);
632 b8822fe7 Christos Stathis
            }
633 b8822fe7 Christos Stathis
634 b8822fe7 Christos Stathis
            @Override
635 b8822fe7 Christos Stathis
            public void onError(Throwable t) {
636 b8822fe7 Christos Stathis
                GWT.log("Error creating pithos", t);
637 3f8622d4 Christos Stathis
                                setError(t);
638 b8822fe7 Christos Stathis
                if (t instanceof RestException)
639 a7c43f26 Christos Stathis
                    displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
640 b8822fe7 Christos Stathis
                else
641 a7c43f26 Christos Stathis
                    displayError("System error Error creating pithos: " + t.getMessage());
642 b8822fe7 Christos Stathis
            }
643 9539e23d Christos Stathis
644 9539e23d Christos Stathis
                        @Override
645 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
646 9539e23d Christos Stathis
                                sessionExpired();
647 9539e23d Christos Stathis
                        }
648 b8822fe7 Christos Stathis
        };
649 b8822fe7 Christos Stathis
        createPithos.setHeader("X-Auth-Token", getToken());
650 b8822fe7 Christos Stathis
        Scheduler.get().scheduleDeferred(createPithos);
651 b8822fe7 Christos Stathis
    }
652 b8822fe7 Christos Stathis
653 64f42c0b Christos Stathis
    protected void createTrashContainer(final Command callback) {
654 7b28ae07 Christos Stathis
        String path = "/" + Pithos.TRASH_CONTAINER;
655 7b28ae07 Christos Stathis
        PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
656 7b28ae07 Christos Stathis
            @Override
657 ebead1b5 Christos Stathis
            public void onSuccess(Resource result) {
658 64f42c0b Christos Stathis
                           fetchAccount(callback);
659 7b28ae07 Christos Stathis
            }
660 7b28ae07 Christos Stathis
661 7b28ae07 Christos Stathis
            @Override
662 7b28ae07 Christos Stathis
            public void onError(Throwable t) {
663 7b28ae07 Christos Stathis
                GWT.log("Error creating pithos", t);
664 3f8622d4 Christos Stathis
                                setError(t);
665 7b28ae07 Christos Stathis
                if (t instanceof RestException)
666 7b28ae07 Christos Stathis
                    displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
667 7b28ae07 Christos Stathis
                else
668 7b28ae07 Christos Stathis
                    displayError("System error Error creating pithos: " + t.getMessage());
669 7b28ae07 Christos Stathis
            }
670 9539e23d Christos Stathis
671 9539e23d Christos Stathis
                        @Override
672 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
673 9539e23d Christos Stathis
                                sessionExpired();
674 9539e23d Christos Stathis
                        }
675 7b28ae07 Christos Stathis
        };
676 7b28ae07 Christos Stathis
        createPithos.setHeader("X-Auth-Token", getToken());
677 7b28ae07 Christos Stathis
        Scheduler.get().scheduleDeferred(createPithos);
678 7b28ae07 Christos Stathis
    }
679 7b28ae07 Christos Stathis
680 7b28ae07 Christos Stathis
    /**
681 a57faaf0 Christos Stathis
         * Creates an HTML fragment that places an image & caption together, for use
682 a57faaf0 Christos Stathis
         * in a group header.
683 a57faaf0 Christos Stathis
         *
684 a57faaf0 Christos Stathis
         * @param imageProto an image prototype for an image
685 a57faaf0 Christos Stathis
         * @param caption the group caption
686 a57faaf0 Christos Stathis
         * @return the header HTML fragment
687 a57faaf0 Christos Stathis
         */
688 a57faaf0 Christos Stathis
        private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
689 a57faaf0 Christos Stathis
                String captionHTML = "<table class='caption' cellpadding='0' " 
690 a57faaf0 Christos Stathis
                + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() 
691 a57faaf0 Christos Stathis
                + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'>&nbsp;" 
692 a57faaf0 Christos Stathis
                + caption + "</b></td></tr></table>";
693 a57faaf0 Christos Stathis
                return captionHTML;
694 a57faaf0 Christos Stathis
        }
695 a57faaf0 Christos Stathis
696 d7be6c23 Christos Stathis
        protected void onWindowResized(int height) {
697 d7be6c23 Christos Stathis
                // Adjust the split panel to take up the available room in the window.
698 0549e173 Christos Stathis
                int newHeight = height - splitPanel.getAbsoluteTop();
699 d7be6c23 Christos Stathis
                if (newHeight < 1)
700 d7be6c23 Christos Stathis
                        newHeight = 1;
701 d7be6c23 Christos Stathis
                splitPanel.setHeight("" + newHeight);
702 d7be6c23 Christos Stathis
                inner.setHeight("" + newHeight);
703 d7be6c23 Christos Stathis
        }
704 a57faaf0 Christos Stathis
705 a57faaf0 Christos Stathis
        @Override
706 a57faaf0 Christos Stathis
        public void onResize(ResizeEvent event) {
707 a57faaf0 Christos Stathis
                int height = event.getHeight();
708 d7be6c23 Christos Stathis
                onWindowResized(height);
709 a57faaf0 Christos Stathis
        }
710 a57faaf0 Christos Stathis
711 a57faaf0 Christos Stathis
        /**
712 a57faaf0 Christos Stathis
         * Display an error message.
713 a57faaf0 Christos Stathis
         *
714 a57faaf0 Christos Stathis
         * @param msg the message to display
715 a57faaf0 Christos Stathis
         */
716 a57faaf0 Christos Stathis
        public void displayError(String msg) {
717 a57faaf0 Christos Stathis
                messagePanel.displayError(msg);
718 a57faaf0 Christos Stathis
        }
719 a57faaf0 Christos Stathis
720 a57faaf0 Christos Stathis
        /**
721 a57faaf0 Christos Stathis
         * Display a warning message.
722 a57faaf0 Christos Stathis
         *
723 a57faaf0 Christos Stathis
         * @param msg the message to display
724 a57faaf0 Christos Stathis
         */
725 a57faaf0 Christos Stathis
        public void displayWarning(String msg) {
726 a57faaf0 Christos Stathis
                messagePanel.displayWarning(msg);
727 a57faaf0 Christos Stathis
        }
728 a57faaf0 Christos Stathis
729 a57faaf0 Christos Stathis
        /**
730 a57faaf0 Christos Stathis
         * Display an informational message.
731 a57faaf0 Christos Stathis
         *
732 a57faaf0 Christos Stathis
         * @param msg the message to display
733 a57faaf0 Christos Stathis
         */
734 a57faaf0 Christos Stathis
        public void displayInformation(String msg) {
735 a57faaf0 Christos Stathis
                messagePanel.displayInformation(msg);
736 a57faaf0 Christos Stathis
        }
737 a57faaf0 Christos Stathis
738 a57faaf0 Christos Stathis
        /**
739 a57faaf0 Christos Stathis
         * Retrieve the fileList.
740 a57faaf0 Christos Stathis
         *
741 a57faaf0 Christos Stathis
         * @return the fileList
742 a57faaf0 Christos Stathis
         */
743 a57faaf0 Christos Stathis
        public FileList getFileList() {
744 a57faaf0 Christos Stathis
                return fileList;
745 a57faaf0 Christos Stathis
        }
746 a57faaf0 Christos Stathis
747 a57faaf0 Christos Stathis
        /**
748 a57faaf0 Christos Stathis
         * Retrieve the topPanel.
749 a57faaf0 Christos Stathis
         *
750 a57faaf0 Christos Stathis
         * @return the topPanel
751 a57faaf0 Christos Stathis
         */
752 a57faaf0 Christos Stathis
        TopPanel getTopPanel() {
753 a57faaf0 Christos Stathis
                return topPanel;
754 a57faaf0 Christos Stathis
        }
755 a57faaf0 Christos Stathis
756 a57faaf0 Christos Stathis
        /**
757 a57faaf0 Christos Stathis
         * Retrieve the clipboard.
758 a57faaf0 Christos Stathis
         *
759 a57faaf0 Christos Stathis
         * @return the clipboard
760 a57faaf0 Christos Stathis
         */
761 a57faaf0 Christos Stathis
        public Clipboard getClipboard() {
762 a57faaf0 Christos Stathis
                return clipboard;
763 a57faaf0 Christos Stathis
        }
764 a57faaf0 Christos Stathis
765 a57faaf0 Christos Stathis
        public StatusPanel getStatusPanel() {
766 a57faaf0 Christos Stathis
                return statusPanel;
767 a57faaf0 Christos Stathis
        }
768 a57faaf0 Christos Stathis
769 a57faaf0 Christos Stathis
        public String getToken() {
770 a57faaf0 Christos Stathis
                return token;
771 a57faaf0 Christos Stathis
        }
772 a57faaf0 Christos Stathis
773 a57faaf0 Christos Stathis
        public static native void preventIESelection() /*-{
774 a57faaf0 Christos Stathis
                $doc.body.onselectstart = function () { return false; };
775 a57faaf0 Christos Stathis
        }-*/;
776 a57faaf0 Christos Stathis
777 a57faaf0 Christos Stathis
        public static native void enableIESelection() /*-{
778 a57faaf0 Christos Stathis
                if ($doc.body.onselectstart != null)
779 a57faaf0 Christos Stathis
                $doc.body.onselectstart = null;
780 a57faaf0 Christos Stathis
        }-*/;
781 a57faaf0 Christos Stathis
782 a57faaf0 Christos Stathis
        /**
783 a57faaf0 Christos Stathis
         * @return the absolute path of the API root URL
784 a57faaf0 Christos Stathis
         */
785 a57faaf0 Christos Stathis
        public String getApiPath() {
786 a57faaf0 Christos Stathis
                Configuration conf = (Configuration) GWT.create(Configuration.class);
787 6084aa02 Christos Stathis
                return conf.apiPath();
788 a57faaf0 Christos Stathis
        }
789 a57faaf0 Christos Stathis
790 a57faaf0 Christos Stathis
        /**
791 a57faaf0 Christos Stathis
         * History support for folder navigation
792 a57faaf0 Christos Stathis
         * adds a new browser history entry
793 a57faaf0 Christos Stathis
         *
794 a57faaf0 Christos Stathis
         * @param key
795 a57faaf0 Christos Stathis
         */
796 a57faaf0 Christos Stathis
        public void updateHistory(String key){
797 a57faaf0 Christos Stathis
//                Replace any whitespace of the initial string to "+"
798 a57faaf0 Christos Stathis
//                String result = key.replaceAll("\\s","+");
799 a57faaf0 Christos Stathis
//                Add a new browser history entry.
800 a57faaf0 Christos Stathis
//                History.newItem(result);
801 a57faaf0 Christos Stathis
                History.newItem(key);
802 a57faaf0 Christos Stathis
        }
803 a57faaf0 Christos Stathis
804 602ebeb4 Christos Stathis
    public void deleteFolder(final Folder folder) {
805 f5023f13 Christos Stathis
        String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
806 602ebeb4 Christos Stathis
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
807 602ebeb4 Christos Stathis
        builder.setHeader("X-Auth-Token", getToken());
808 602ebeb4 Christos Stathis
        try {
809 602ebeb4 Christos Stathis
            builder.sendRequest("", new RequestCallback() {
810 602ebeb4 Christos Stathis
                @Override
811 ebead1b5 Christos Stathis
                public void onResponseReceived(Request request, Response response) {
812 602ebeb4 Christos Stathis
                    if (response.getStatusCode() == Response.SC_OK) {
813 602ebeb4 Christos Stathis
                        JSONValue json = JSONParser.parseStrict(response.getText());
814 602ebeb4 Christos Stathis
                        JSONArray array = json.isArray();
815 602ebeb4 Christos Stathis
                        int i = 0;
816 602ebeb4 Christos Stathis
                        if (array != null) {
817 602ebeb4 Christos Stathis
                            deleteObject(folder, i, array);
818 602ebeb4 Christos Stathis
                        }
819 602ebeb4 Christos Stathis
                    }
820 602ebeb4 Christos Stathis
                }
821 602ebeb4 Christos Stathis
822 602ebeb4 Christos Stathis
                @Override
823 ebead1b5 Christos Stathis
                public void onError(Request request, Throwable exception) {
824 3f8622d4 Christos Stathis
                        setError(exception);
825 a7c43f26 Christos Stathis
                    displayError("System error unable to delete folder: " + exception.getMessage());
826 602ebeb4 Christos Stathis
                }
827 602ebeb4 Christos Stathis
            });
828 602ebeb4 Christos Stathis
        }
829 602ebeb4 Christos Stathis
        catch (RequestException e) {
830 602ebeb4 Christos Stathis
        }
831 602ebeb4 Christos Stathis
    }
832 602ebeb4 Christos Stathis
833 64f42c0b Christos Stathis
    void deleteObject(final Folder folder, final int i, final JSONArray array) {
834 602ebeb4 Christos Stathis
        if (i < array.size()) {
835 602ebeb4 Christos Stathis
            JSONObject o = array.get(i).isObject();
836 602ebeb4 Christos Stathis
            if (o != null && !o.containsKey("subdir")) {
837 602ebeb4 Christos Stathis
                JSONString name = o.get("name").isString();
838 b51c628b Christos Stathis
                String path = "/" + folder.getContainer() + "/" + name.stringValue();
839 5f91f72d Christos Stathis
                DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), URL.encode(path)) {
840 602ebeb4 Christos Stathis
                    @Override
841 ebead1b5 Christos Stathis
                    public void onSuccess(Resource result) {
842 602ebeb4 Christos Stathis
                        deleteObject(folder, i + 1, array);
843 602ebeb4 Christos Stathis
                    }
844 602ebeb4 Christos Stathis
845 602ebeb4 Christos Stathis
                    @Override
846 602ebeb4 Christos Stathis
                    public void onError(Throwable t) {
847 602ebeb4 Christos Stathis
                        GWT.log("", t);
848 3f8622d4 Christos Stathis
                                                setError(t);
849 a7c43f26 Christos Stathis
                        displayError("System error unable to delete folder: " + t.getMessage());
850 602ebeb4 Christos Stathis
                    }
851 9539e23d Christos Stathis
852 9539e23d Christos Stathis
                                    @Override
853 9539e23d Christos Stathis
                                    protected void onUnauthorized(Response response) {
854 9539e23d Christos Stathis
                                            sessionExpired();
855 9539e23d Christos Stathis
                                    }
856 602ebeb4 Christos Stathis
                };
857 602ebeb4 Christos Stathis
                delete.setHeader("X-Auth-Token", getToken());
858 602ebeb4 Christos Stathis
                Scheduler.get().scheduleDeferred(delete);
859 602ebeb4 Christos Stathis
            }
860 7811b9d1 Christos Stathis
            else if (o != null) {
861 602ebeb4 Christos Stathis
                String subdir = o.get("subdir").isString().stringValue();
862 602ebeb4 Christos Stathis
                subdir = subdir.substring(0, subdir.length() - 1);
863 f5023f13 Christos Stathis
                String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
864 602ebeb4 Christos Stathis
                RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
865 602ebeb4 Christos Stathis
                builder.setHeader("X-Auth-Token", getToken());
866 602ebeb4 Christos Stathis
                try {
867 602ebeb4 Christos Stathis
                    builder.sendRequest("", new RequestCallback() {
868 602ebeb4 Christos Stathis
                        @Override
869 ebead1b5 Christos Stathis
                        public void onResponseReceived(Request request, Response response) {
870 602ebeb4 Christos Stathis
                            if (response.getStatusCode() == Response.SC_OK) {
871 602ebeb4 Christos Stathis
                                JSONValue json = JSONParser.parseStrict(response.getText());
872 602ebeb4 Christos Stathis
                                JSONArray array2 = json.isArray();
873 602ebeb4 Christos Stathis
                                if (array2 != null) {
874 602ebeb4 Christos Stathis
                                    int l = array.size();
875 602ebeb4 Christos Stathis
                                    for (int j=0; j<array2.size(); j++) {
876 602ebeb4 Christos Stathis
                                        array.set(l++, array2.get(j));
877 602ebeb4 Christos Stathis
                                    }
878 602ebeb4 Christos Stathis
                                }
879 602ebeb4 Christos Stathis
                                deleteObject(folder, i + 1, array);
880 602ebeb4 Christos Stathis
                            }
881 602ebeb4 Christos Stathis
                        }
882 602ebeb4 Christos Stathis
883 602ebeb4 Christos Stathis
                        @Override
884 ebead1b5 Christos Stathis
                        public void onError(Request request, Throwable exception) {
885 3f8622d4 Christos Stathis
                                setError(exception);
886 a7c43f26 Christos Stathis
                            displayError("System error unable to delete folder: " + exception.getMessage());
887 602ebeb4 Christos Stathis
                        }
888 602ebeb4 Christos Stathis
                    });
889 602ebeb4 Christos Stathis
                }
890 602ebeb4 Christos Stathis
                catch (RequestException e) {
891 602ebeb4 Christos Stathis
                }
892 602ebeb4 Christos Stathis
            }
893 602ebeb4 Christos Stathis
        }
894 602ebeb4 Christos Stathis
        else {
895 b51c628b Christos Stathis
            String path = folder.getUri();
896 5f91f72d Christos Stathis
            DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), URL.encode(path)) {
897 602ebeb4 Christos Stathis
                @Override
898 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
899 64f42c0b Christos Stathis
                    updateFolder(folder.getParent(), true, new Command() {
900 64f42c0b Christos Stathis
                                                
901 64f42c0b Christos Stathis
                                                @Override
902 64f42c0b Christos Stathis
                                                public void execute() {
903 64f42c0b Christos Stathis
                                                        updateStatistics();
904 64f42c0b Christos Stathis
                                                }
905 64f42c0b Christos Stathis
                                        });
906 602ebeb4 Christos Stathis
                }
907 602ebeb4 Christos Stathis
908 602ebeb4 Christos Stathis
                @Override
909 602ebeb4 Christos Stathis
                public void onError(Throwable t) {
910 602ebeb4 Christos Stathis
                    GWT.log("", t);
911 3f8622d4 Christos Stathis
                                        setError(t);
912 602ebeb4 Christos Stathis
                    if (t instanceof RestException) {
913 7b28ae07 Christos Stathis
                            if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
914 7b28ae07 Christos Stathis
                                    displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
915 7b28ae07 Christos Stathis
                            else
916 7b28ae07 Christos Stathis
                                    onSuccess(null);
917 602ebeb4 Christos Stathis
                    }
918 602ebeb4 Christos Stathis
                    else
919 a7c43f26 Christos Stathis
                        displayError("System error unable to delete folder: " + t.getMessage());
920 602ebeb4 Christos Stathis
                }
921 9539e23d Christos Stathis
922 9539e23d Christos Stathis
                                @Override
923 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
924 9539e23d Christos Stathis
                                        sessionExpired();
925 9539e23d Christos Stathis
                                }
926 602ebeb4 Christos Stathis
            };
927 602ebeb4 Christos Stathis
            deleteFolder.setHeader("X-Auth-Token", getToken());
928 602ebeb4 Christos Stathis
            Scheduler.get().scheduleDeferred(deleteFolder);
929 602ebeb4 Christos Stathis
        }
930 602ebeb4 Christos Stathis
    }
931 fad48f0d Christos Stathis
932 fad48f0d Christos Stathis
    public FolderTreeView getFolderTreeView() {
933 fad48f0d Christos Stathis
        return folderTreeView;
934 fad48f0d Christos Stathis
    }
935 d6be39da Christos Stathis
936 63ee965c Christos Stathis
    public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
937 d6be39da Christos Stathis
        if (iter.hasNext()) {
938 d6be39da Christos Stathis
            File file = iter.next();
939 b51c628b Christos Stathis
            String path = targetUri + "/" + file.getName();
940 63ee965c Christos Stathis
            PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
941 d6be39da Christos Stathis
                @Override
942 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
943 63ee965c Christos Stathis
                    copyFiles(iter, targetUsername, targetUri, callback);
944 d6be39da Christos Stathis
                }
945 d6be39da Christos Stathis
946 d6be39da Christos Stathis
                @Override
947 d6be39da Christos Stathis
                public void onError(Throwable t) {
948 d6be39da Christos Stathis
                    GWT.log("", t);
949 3f8622d4 Christos Stathis
                                        setError(t);
950 d6be39da Christos Stathis
                    if (t instanceof RestException) {
951 a7c43f26 Christos Stathis
                        displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
952 d6be39da Christos Stathis
                    }
953 d6be39da Christos Stathis
                    else
954 a7c43f26 Christos Stathis
                        displayError("System error unable to copy file: "+t.getMessage());
955 d6be39da Christos Stathis
                }
956 9539e23d Christos Stathis
957 9539e23d Christos Stathis
                                @Override
958 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
959 9539e23d Christos Stathis
                                        sessionExpired();
960 9539e23d Christos Stathis
                                }
961 d6be39da Christos Stathis
            };
962 d6be39da Christos Stathis
            copyFile.setHeader("X-Auth-Token", getToken());
963 a2411896 Christos Stathis
            copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
964 43c372e8 Christos Stathis
            if (!file.getOwner().equals(targetUsername))
965 a2411896 Christos Stathis
                    copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwner()));
966 e5af9268 Christos Stathis
            copyFile.setHeader("Content-Type", file.getContentType());
967 d6be39da Christos Stathis
            Scheduler.get().scheduleDeferred(copyFile);
968 d6be39da Christos Stathis
        }
969 d6be39da Christos Stathis
        else  if (callback != null) {
970 d6be39da Christos Stathis
            callback.execute();
971 d6be39da Christos Stathis
        }
972 d6be39da Christos Stathis
    }
973 d6be39da Christos Stathis
974 63ee965c Christos Stathis
    public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
975 d6be39da Christos Stathis
        if (iter.hasNext()) {
976 d6be39da Christos Stathis
            final Folder f = iter.next();
977 acbb73ac Christos Stathis
            copyFolder(f, targetUsername, targetUri, new Command() {
978 acbb73ac Christos Stathis
                                
979 acbb73ac Christos Stathis
                                @Override
980 acbb73ac Christos Stathis
                                public void execute() {
981 acbb73ac Christos Stathis
                                        copySubfolders(iter, targetUsername, targetUri, callback);
982 acbb73ac Christos Stathis
                                }
983 acbb73ac Christos Stathis
                        });
984 d6be39da Christos Stathis
        }
985 d6be39da Christos Stathis
        else  if (callback != null) {
986 d6be39da Christos Stathis
            callback.execute();
987 d6be39da Christos Stathis
        }
988 d6be39da Christos Stathis
    }
989 d6be39da Christos Stathis
990 63ee965c Christos Stathis
    public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
991 b51c628b Christos Stathis
        String path = targetUri + "/" + f.getName();
992 63ee965c Christos Stathis
        PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
993 d6be39da Christos Stathis
            @Override
994 ebead1b5 Christos Stathis
            public void onSuccess(Resource result) {
995 f5023f13 Christos Stathis
                    GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
996 acbb73ac Christos Stathis
997 acbb73ac Christos Stathis
                                        @Override
998 abd8738f Christos Stathis
                                        public void onSuccess(final Folder _f) {
999 abd8738f Christos Stathis
                                Iterator<File> iter = _f.getFiles().iterator();
1000 abd8738f Christos Stathis
                                copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
1001 acbb73ac Christos Stathis
                                    @Override
1002 acbb73ac Christos Stathis
                                    public void execute() {
1003 abd8738f Christos Stathis
                                        Iterator<Folder> iterf = _f.getSubfolders().iterator();
1004 abd8738f Christos Stathis
                                        copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1005 acbb73ac Christos Stathis
                                    }
1006 acbb73ac Christos Stathis
                                });
1007 acbb73ac Christos Stathis
                                        }
1008 acbb73ac Christos Stathis
1009 acbb73ac Christos Stathis
                                        @Override
1010 acbb73ac Christos Stathis
                                        public void onError(Throwable t) {
1011 acbb73ac Christos Stathis
                                GWT.log("", t);
1012 3f8622d4 Christos Stathis
                                                setError(t);
1013 acbb73ac Christos Stathis
                                if (t instanceof RestException) {
1014 acbb73ac Christos Stathis
                                    displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1015 acbb73ac Christos Stathis
                                }
1016 acbb73ac Christos Stathis
                                else
1017 acbb73ac Christos Stathis
                                    displayError("System error getting folder: " + t.getMessage());
1018 acbb73ac Christos Stathis
                                        }
1019 9539e23d Christos Stathis
1020 9539e23d Christos Stathis
                                        @Override
1021 9539e23d Christos Stathis
                                        protected void onUnauthorized(Response response) {
1022 9539e23d Christos Stathis
                                                sessionExpired();
1023 9539e23d Christos Stathis
                                        }
1024 acbb73ac Christos Stathis
                                };
1025 acbb73ac Christos Stathis
                                getFolder.setHeader("X-Auth-Token", getToken());
1026 acbb73ac Christos Stathis
                                Scheduler.get().scheduleDeferred(getFolder);
1027 d6be39da Christos Stathis
            }
1028 d6be39da Christos Stathis
1029 d6be39da Christos Stathis
            @Override
1030 d6be39da Christos Stathis
            public void onError(Throwable t) {
1031 d6be39da Christos Stathis
                GWT.log("", t);
1032 3f8622d4 Christos Stathis
                                setError(t);
1033 3f8622d4 Christos Stathis
               if (t instanceof RestException) {
1034 acbb73ac Christos Stathis
                    displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1035 d6be39da Christos Stathis
                }
1036 d6be39da Christos Stathis
                else
1037 acbb73ac Christos Stathis
                    displayError("System error creating folder: " + t.getMessage());
1038 d6be39da Christos Stathis
            }
1039 9539e23d Christos Stathis
1040 9539e23d Christos Stathis
                        @Override
1041 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
1042 9539e23d Christos Stathis
                                sessionExpired();
1043 9539e23d Christos Stathis
                        }
1044 d6be39da Christos Stathis
        };
1045 d6be39da Christos Stathis
        createFolder.setHeader("X-Auth-Token", getToken());
1046 d6be39da Christos Stathis
        createFolder.setHeader("Accept", "*/*");
1047 d6be39da Christos Stathis
        createFolder.setHeader("Content-Length", "0");
1048 d6be39da Christos Stathis
        createFolder.setHeader("Content-Type", "application/folder");
1049 d6be39da Christos Stathis
        Scheduler.get().scheduleDeferred(createFolder);
1050 d6be39da Christos Stathis
    }
1051 105b4e49 Christos Stathis
    
1052 7811b9d1 Christos Stathis
    public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1053 105b4e49 Christos Stathis
            selectionModels.add(model);
1054 105b4e49 Christos Stathis
    }
1055 6d8e0f58 Christos Stathis
1056 6d8e0f58 Christos Stathis
        public OtherSharedTreeView getOtherSharedTreeView() {
1057 6d8e0f58 Christos Stathis
                return otherSharedTreeView;
1058 6d8e0f58 Christos Stathis
        }
1059 16e8478a Christos Stathis
1060 16e8478a Christos Stathis
        public void updateTrash(boolean showFiles, Command callback) {
1061 16e8478a Christos Stathis
                updateFolder(trash, showFiles, callback);
1062 16e8478a Christos Stathis
        }
1063 4baffab1 Christos Stathis
1064 4baffab1 Christos Stathis
        public void updateGroupsNode() {
1065 4baffab1 Christos Stathis
                groupTreeView.updateGroupNode(null);
1066 4baffab1 Christos Stathis
        }
1067 4baffab1 Christos Stathis
1068 4baffab1 Christos Stathis
        public void addGroup(String groupname) {
1069 4baffab1 Christos Stathis
                Group newGroup = new Group(groupname);
1070 4baffab1 Christos Stathis
                account.addGroup(newGroup);
1071 4baffab1 Christos Stathis
                groupTreeView.updateGroupNode(null);
1072 4baffab1 Christos Stathis
        }
1073 61f8605d Christos Stathis
1074 61f8605d Christos Stathis
        public void removeGroup(Group group) {
1075 61f8605d Christos Stathis
                account.removeGroup(group);
1076 61f8605d Christos Stathis
                updateGroupsNode();
1077 61f8605d Christos Stathis
        }
1078 f1b24b82 Christos Stathis
1079 f1b24b82 Christos Stathis
        public TreeView getSelectedTree() {
1080 f1b24b82 Christos Stathis
                return selectedTree;
1081 f1b24b82 Christos Stathis
        }
1082 f1b24b82 Christos Stathis
        
1083 f1b24b82 Christos Stathis
        public Folder getSelection() {
1084 f1b24b82 Christos Stathis
                return selectedTree.getSelection();
1085 f1b24b82 Christos Stathis
        }
1086 a19f758c Christos Stathis
1087 a19f758c Christos Stathis
        public void showFolderStatistics(int folderFileCount) {
1088 a19f758c Christos Stathis
                numOfFiles.setHTML(String.valueOf(folderFileCount));
1089 a19f758c Christos Stathis
        }
1090 b162dfaa Christos Stathis
1091 b162dfaa Christos Stathis
        public GroupTreeView getGroupTreeView() {
1092 b162dfaa Christos Stathis
                return groupTreeView;
1093 b162dfaa Christos Stathis
        }
1094 9539e23d Christos Stathis
1095 9539e23d Christos Stathis
        public void sessionExpired() {
1096 9539e23d Christos Stathis
                new SessionExpiredDialog(this).center();
1097 9539e23d Christos Stathis
        }
1098 340bf0d6 Christos Stathis
1099 340bf0d6 Christos Stathis
        public void updateRootFolder(Command callback) {
1100 340bf0d6 Christos Stathis
                updateFolder(account.getPithos(), false, callback);
1101 340bf0d6 Christos Stathis
        }
1102 7c54d867 Christos Stathis
1103 7c54d867 Christos Stathis
        void createMySharedTree() {
1104 7c54d867 Christos Stathis
                mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1105 7c54d867 Christos Stathis
                mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1106 7c54d867 Christos Stathis
                    @Override
1107 ebead1b5 Christos Stathis
                    public void onSelectionChange(SelectionChangeEvent event) {
1108 7c54d867 Christos Stathis
                        if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1109 7c54d867 Christos Stathis
                            deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1110 7c54d867 Christos Stathis
                            upload.setEnabled(false);
1111 7c54d867 Christos Stathis
                            updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1112 7c54d867 Christos Stathis
                        }
1113 b75210b5 Christos Stathis
                        showRelevantToolbarButtons();
1114 d7be6c23 Christos Stathis
                     }
1115 7c54d867 Christos Stathis
                });
1116 7c54d867 Christos Stathis
                selectionModels.add(mysharedTreeSelectionModel);
1117 7c54d867 Christos Stathis
                mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1118 7c54d867 Christos Stathis
                mysharedTreeViewModel.initialize(new Command() {
1119 7c54d867 Christos Stathis
                        
1120 7c54d867 Christos Stathis
                        @Override
1121 7c54d867 Christos Stathis
                        public void execute() {
1122 7c54d867 Christos Stathis
                            mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1123 0549e173 Christos Stathis
                                trees.insert(mysharedTreeView, 2);
1124 0549e173 Christos Stathis
                                treeViews.add(mysharedTreeView);
1125 7c54d867 Christos Stathis
                                createOtherSharedTree();
1126 7c54d867 Christos Stathis
                        }
1127 7c54d867 Christos Stathis
                });
1128 7c54d867 Christos Stathis
        }
1129 7c54d867 Christos Stathis
1130 7c54d867 Christos Stathis
        void createOtherSharedTree() {
1131 7c54d867 Christos Stathis
                otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1132 7c54d867 Christos Stathis
                otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1133 7c54d867 Christos Stathis
                    @Override
1134 ebead1b5 Christos Stathis
                    public void onSelectionChange(SelectionChangeEvent event) {
1135 7c54d867 Christos Stathis
                        if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1136 7c54d867 Christos Stathis
                            deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1137 0549e173 Christos Stathis
                            otherSharedTreeView.addStyleName("cellTreeWidget-selectedTree");
1138 7c54d867 Christos Stathis
                            applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1139 7c54d867 Christos Stathis
                            updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1140 7c54d867 Christos Stathis
                        }
1141 b75210b5 Christos Stathis
                        showRelevantToolbarButtons();
1142 d7be6c23 Christos Stathis
                     }
1143 7c54d867 Christos Stathis
                });
1144 7c54d867 Christos Stathis
                selectionModels.add(otherSharedTreeSelectionModel);
1145 7c54d867 Christos Stathis
                otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1146 7c54d867 Christos Stathis
                otherSharedTreeViewModel.initialize(new Command() {
1147 7c54d867 Christos Stathis
                        
1148 7c54d867 Christos Stathis
                        @Override
1149 7c54d867 Christos Stathis
                        public void execute() {
1150 7c54d867 Christos Stathis
                            otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1151 0549e173 Christos Stathis
                                trees.insert(otherSharedTreeView, 3);
1152 0549e173 Christos Stathis
                                treeViews.add(otherSharedTreeView);
1153 7c54d867 Christos Stathis
                        }
1154 7c54d867 Christos Stathis
                });
1155 7c54d867 Christos Stathis
        }
1156 4ac4833e Christos Stathis
1157 c758f656 Christos Stathis
        public native void log1(String message)/*-{
1158 c758f656 Christos Stathis
                $wnd.console.log(message);
1159 c758f656 Christos Stathis
        }-*/;
1160 3f8622d4 Christos Stathis
1161 3f8622d4 Christos Stathis
        public String getErrorData() {
1162 3f8622d4 Christos Stathis
                if (error != null)
1163 3f8622d4 Christos Stathis
                        return error.toString();
1164 3f8622d4 Christos Stathis
                return "";
1165 3f8622d4 Christos Stathis
        }
1166 3f8622d4 Christos Stathis
        
1167 3f8622d4 Christos Stathis
        public void setError(Throwable t) {
1168 3f8622d4 Christos Stathis
                error = t;
1169 3f8622d4 Christos Stathis
        }
1170 b75210b5 Christos Stathis
        
1171 b75210b5 Christos Stathis
        public void showRelevantToolbarButtons() {
1172 b75210b5 Christos Stathis
                toolbar.showRelevantButtons();
1173 b75210b5 Christos Stathis
        }
1174 1a0db4f4 Christos Stathis
1175 1a0db4f4 Christos Stathis
        public FileUploadDialog getFileUploadDialog() {
1176 1a0db4f4 Christos Stathis
                if (fileUploadDialog == null)
1177 1a0db4f4 Christos Stathis
                        fileUploadDialog = new FileUploadDialog(this);
1178 1a0db4f4 Christos Stathis
                return fileUploadDialog;
1179 1a0db4f4 Christos Stathis
        }
1180 1a0db4f4 Christos Stathis
1181 1a0db4f4 Christos Stathis
        public void hideUploadIndicator() {
1182 1a0db4f4 Christos Stathis
                upload.removeStyleName("pithos-uploadButton-loading");
1183 1a0db4f4 Christos Stathis
                upload.setTitle("");
1184 1a0db4f4 Christos Stathis
        }
1185 1a0db4f4 Christos Stathis
        
1186 1a0db4f4 Christos Stathis
        public void showUploadIndicator() {
1187 1a0db4f4 Christos Stathis
                upload.addStyleName("pithos-uploadButton-loading");
1188 1a0db4f4 Christos Stathis
                upload.setTitle("Upload in progress. Click for details.");
1189 1a0db4f4 Christos Stathis
        }
1190 a2f617f8 Christos Stathis
1191 a2f617f8 Christos Stathis
        public void scheduleFolderHeadCommand(final Folder folder, final Command callback) {
1192 a2f617f8 Christos Stathis
                HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, getApiPath(), folder.getOwner(), folder.getUri(), folder) {
1193 a2f617f8 Christos Stathis
1194 a2f617f8 Christos Stathis
                        @Override
1195 a2f617f8 Christos Stathis
                        public void onSuccess(Folder _result) {
1196 a2f617f8 Christos Stathis
                                if (callback != null)
1197 a2f617f8 Christos Stathis
                                        callback.execute();
1198 a2f617f8 Christos Stathis
                        }
1199 a2f617f8 Christos Stathis
1200 a2f617f8 Christos Stathis
                        @Override
1201 a2f617f8 Christos Stathis
                        public void onError(Throwable t) {
1202 a2f617f8 Christos Stathis
                        if (t instanceof RestException) {
1203 a2f617f8 Christos Stathis
                                if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
1204 a2f617f8 Christos Stathis
                        final String path = folder.getUri();
1205 a2f617f8 Christos Stathis
                        PutRequest newFolder = new PutRequest(getApiPath(), folder.getOwner(), path) {
1206 a2f617f8 Christos Stathis
                            @Override
1207 a2f617f8 Christos Stathis
                            public void onSuccess(Resource _result) {
1208 a2f617f8 Christos Stathis
                                    scheduleFolderHeadCommand(folder, callback);
1209 a2f617f8 Christos Stathis
                            }
1210 a2f617f8 Christos Stathis
1211 a2f617f8 Christos Stathis
                            @Override
1212 a2f617f8 Christos Stathis
                            public void onError(Throwable _t) {
1213 a2f617f8 Christos Stathis
                                GWT.log("", _t);
1214 a2f617f8 Christos Stathis
                                                        setError(_t);
1215 a2f617f8 Christos Stathis
                                if(_t instanceof RestException){
1216 a2f617f8 Christos Stathis
                                    displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
1217 a2f617f8 Christos Stathis
                                }
1218 a2f617f8 Christos Stathis
                                else
1219 a2f617f8 Christos Stathis
                                    displayError("System error creating folder: " + _t.getMessage());
1220 a2f617f8 Christos Stathis
                            }
1221 a2f617f8 Christos Stathis
1222 a2f617f8 Christos Stathis
                                            @Override
1223 a2f617f8 Christos Stathis
                                            protected void onUnauthorized(Response response) {
1224 a2f617f8 Christos Stathis
                                                    sessionExpired();
1225 a2f617f8 Christos Stathis
                                            }
1226 a2f617f8 Christos Stathis
                        };
1227 a2f617f8 Christos Stathis
                        newFolder.setHeader("X-Auth-Token", getToken());
1228 a2f617f8 Christos Stathis
                        newFolder.setHeader("Content-Type", "application/folder");
1229 a2f617f8 Christos Stathis
                        newFolder.setHeader("Accept", "*/*");
1230 a2f617f8 Christos Stathis
                        newFolder.setHeader("Content-Length", "0");
1231 a2f617f8 Christos Stathis
                        Scheduler.get().scheduleDeferred(newFolder);
1232 a2f617f8 Christos Stathis
                                }
1233 a2f617f8 Christos Stathis
                                else
1234 a2f617f8 Christos Stathis
                                        displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
1235 a2f617f8 Christos Stathis
                        }
1236 a2f617f8 Christos Stathis
                        else
1237 a2f617f8 Christos Stathis
                            displayError("System error heading folder: " + t.getMessage());
1238 a2f617f8 Christos Stathis
1239 a2f617f8 Christos Stathis
                        GWT.log("Error heading folder", t);
1240 a2f617f8 Christos Stathis
                                setError(t);
1241 a2f617f8 Christos Stathis
                        }
1242 a2f617f8 Christos Stathis
1243 a2f617f8 Christos Stathis
                        @Override
1244 a2f617f8 Christos Stathis
                        protected void onUnauthorized(Response response) {
1245 a2f617f8 Christos Stathis
                                sessionExpired();
1246 a2f617f8 Christos Stathis
                        }
1247 a2f617f8 Christos Stathis
                };
1248 a2f617f8 Christos Stathis
                headFolder.setHeader("X-Auth-Token", getToken());
1249 a2f617f8 Christos Stathis
                Scheduler.get().scheduleDeferred(headFolder);
1250 a2f617f8 Christos Stathis
        }
1251 a2f617f8 Christos Stathis
1252 a2f617f8 Christos Stathis
        public void scheduleFileHeadCommand(File f, final Command callback) {
1253 a2f617f8 Christos Stathis
                HeadRequest<File> headFile = new HeadRequest<File>(File.class, getApiPath(), f.getOwner(), f.getUri(), f) {
1254 a2f617f8 Christos Stathis
1255 a2f617f8 Christos Stathis
                        @Override
1256 a2f617f8 Christos Stathis
                        public void onSuccess(File _result) {
1257 a2f617f8 Christos Stathis
                                if (callback != null)
1258 a2f617f8 Christos Stathis
                                        callback.execute();
1259 a2f617f8 Christos Stathis
                        }
1260 a2f617f8 Christos Stathis
1261 a2f617f8 Christos Stathis
                        @Override
1262 a2f617f8 Christos Stathis
                        public void onError(Throwable t) {
1263 a2f617f8 Christos Stathis
                        GWT.log("Error heading file", t);
1264 a2f617f8 Christos Stathis
                                setError(t);
1265 a2f617f8 Christos Stathis
                        if (t instanceof RestException)
1266 a2f617f8 Christos Stathis
                            displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
1267 a2f617f8 Christos Stathis
                        else
1268 a2f617f8 Christos Stathis
                            displayError("System error heading file: " + t.getMessage());
1269 a2f617f8 Christos Stathis
                        }
1270 a2f617f8 Christos Stathis
1271 a2f617f8 Christos Stathis
                        @Override
1272 a2f617f8 Christos Stathis
                        protected void onUnauthorized(Response response) {
1273 a2f617f8 Christos Stathis
                                sessionExpired();
1274 a2f617f8 Christos Stathis
                        }
1275 a2f617f8 Christos Stathis
                };
1276 a2f617f8 Christos Stathis
                headFile.setHeader("X-Auth-Token", getToken());
1277 a2f617f8 Christos Stathis
                Scheduler.get().scheduleDeferred(headFile);
1278 a2f617f8 Christos Stathis
        }
1279 a57faaf0 Christos Stathis
}