Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (27.9 kB)

1 a57faaf0 Christos Stathis
/*
2 63366925 Christos Stathis
 * Copyright 2011 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 6084aa02 Christos Stathis
import com.google.gwt.core.client.Scheduler;
38 6084aa02 Christos Stathis
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
39 602ebeb4 Christos Stathis
import com.google.gwt.http.client.Request;
40 602ebeb4 Christos Stathis
import com.google.gwt.http.client.RequestBuilder;
41 602ebeb4 Christos Stathis
import com.google.gwt.http.client.RequestCallback;
42 602ebeb4 Christos Stathis
import com.google.gwt.http.client.RequestException;
43 602ebeb4 Christos Stathis
import com.google.gwt.http.client.Response;
44 602ebeb4 Christos Stathis
import com.google.gwt.json.client.JSONArray;
45 602ebeb4 Christos Stathis
import com.google.gwt.json.client.JSONObject;
46 602ebeb4 Christos Stathis
import com.google.gwt.json.client.JSONParser;
47 602ebeb4 Christos Stathis
import com.google.gwt.json.client.JSONString;
48 602ebeb4 Christos Stathis
import com.google.gwt.json.client.JSONValue;
49 d6be39da Christos Stathis
import com.google.gwt.user.client.Command;
50 875a0179 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
51 0d1e8310 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
52 875a0179 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
53 6084aa02 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.AccountResource;
54 875a0179 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
55 7c818c14 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
56 6084aa02 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
57 7c818c14 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
58 602ebeb4 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
59 602ebeb4 Christos Stathis
import gr.grnet.pithos.web.client.rest.DeleteRequest;
60 6084aa02 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
61 d6be39da Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
62 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
63 a57faaf0 Christos Stathis
64 a6592a15 Christos Stathis
import gr.grnet.pithos.web.client.tagtree.Tag;
65 a6592a15 Christos Stathis
import gr.grnet.pithos.web.client.tagtree.TagTreeView;
66 a6592a15 Christos Stathis
import gr.grnet.pithos.web.client.tagtree.TagTreeViewModel;
67 a57faaf0 Christos Stathis
import java.util.ArrayList;
68 a57faaf0 Christos Stathis
import java.util.Arrays;
69 a57faaf0 Christos Stathis
import java.util.Date;
70 a57faaf0 Christos Stathis
import java.util.HashMap;
71 7c818c14 Christos Stathis
import java.util.Iterator;
72 a57faaf0 Christos Stathis
import java.util.List;
73 a57faaf0 Christos Stathis
74 a57faaf0 Christos Stathis
import com.google.gwt.core.client.EntryPoint;
75 a57faaf0 Christos Stathis
import com.google.gwt.core.client.GWT;
76 a57faaf0 Christos Stathis
import com.google.gwt.event.logical.shared.ResizeEvent;
77 a57faaf0 Christos Stathis
import com.google.gwt.event.logical.shared.ResizeHandler;
78 a57faaf0 Christos Stathis
import com.google.gwt.event.logical.shared.SelectionEvent;
79 a57faaf0 Christos Stathis
import com.google.gwt.event.logical.shared.SelectionHandler;
80 a57faaf0 Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat;
81 a57faaf0 Christos Stathis
import com.google.gwt.resources.client.ClientBundle;
82 a57faaf0 Christos Stathis
import com.google.gwt.resources.client.ImageResource;
83 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Cookies;
84 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Event;
85 a57faaf0 Christos Stathis
import com.google.gwt.user.client.History;
86 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Window;
87 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
88 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.DecoratedTabPanel;
89 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
90 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
91 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.RootPanel;
92 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.TabPanel;
93 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
94 875a0179 Christos Stathis
import java.util.Set;
95 6084aa02 Christos Stathis
96 a57faaf0 Christos Stathis
/**
97 a57faaf0 Christos Stathis
 * Entry point classes define <code>onModuleLoad()</code>.
98 a57faaf0 Christos Stathis
 */
99 749068ba Christos Stathis
public class Pithos implements EntryPoint, ResizeHandler {
100 a57faaf0 Christos Stathis
101 a57faaf0 Christos Stathis
        /**
102 a57faaf0 Christos Stathis
         * A constant that denotes the completion of an IncrementalCommand.
103 a57faaf0 Christos Stathis
         */
104 a57faaf0 Christos Stathis
        public static final boolean DONE = false;
105 a57faaf0 Christos Stathis
106 a57faaf0 Christos Stathis
        public static final int VISIBLE_FILE_COUNT = 25;
107 a57faaf0 Christos Stathis
108 a57faaf0 Christos Stathis
        /**
109 a57faaf0 Christos Stathis
         * Instantiate an application-level image bundle. This object will provide
110 a57faaf0 Christos Stathis
         * programmatic access to all the images needed by widgets.
111 a57faaf0 Christos Stathis
         */
112 a57faaf0 Christos Stathis
        private static Images images = (Images) GWT.create(Images.class);
113 a57faaf0 Christos Stathis
114 6084aa02 Christos Stathis
    public String getUsername() {
115 6084aa02 Christos Stathis
        return username;
116 6084aa02 Christos Stathis
    }
117 a57faaf0 Christos Stathis
118 8e61880b Christos Stathis
    public void setAccount(AccountResource acct) {
119 8e61880b Christos Stathis
        account = acct;
120 8e61880b Christos Stathis
    }
121 8e61880b Christos Stathis
122 8e61880b Christos Stathis
    public AccountResource getAccount() {
123 8e61880b Christos Stathis
        return account;
124 8e61880b Christos Stathis
    }
125 8e61880b Christos Stathis
126 5ebc9312 Christos Stathis
    public void updateFolder(Folder f, boolean showfiles) {
127 5ebc9312 Christos Stathis
        folderTreeView.updateFolder(f, showfiles);
128 b651e67f Christos Stathis
    }
129 b651e67f Christos Stathis
130 0d1e8310 Christos Stathis
    public void updateTag(Tag t) {
131 0d1e8310 Christos Stathis
        tagTreeView.updateTag(t);
132 0d1e8310 Christos Stathis
    }
133 0d1e8310 Christos Stathis
134 0d1e8310 Christos Stathis
    public void updateTags() {
135 ef5b664d Christos Stathis
        tagTreeViewModel.initialize(getAllTags());
136 ef5b664d Christos Stathis
    }
137 ef5b664d Christos Stathis
138 ef5b664d Christos Stathis
    public List<Tag> getAllTags() {
139 ef5b664d Christos Stathis
        List<Tag> tagList = new ArrayList<Tag>();
140 ef5b664d Christos Stathis
        for (Folder f : account.getContainers()) {
141 ef5b664d Christos Stathis
            for (String t : f.getTags()) {
142 ef5b664d Christos Stathis
                tagList.add(new Tag(t));
143 ef5b664d Christos Stathis
            }
144 ef5b664d Christos Stathis
        }
145 ef5b664d Christos Stathis
        return tagList;
146 0d1e8310 Christos Stathis
    }
147 0d1e8310 Christos Stathis
148 6084aa02 Christos Stathis
    /**
149 a57faaf0 Christos Stathis
         * An aggregate image bundle that pulls together all the images for this
150 a57faaf0 Christos Stathis
         * application into a single bundle.
151 a57faaf0 Christos Stathis
         */
152 36dc8e18 Christos Stathis
        public interface Images extends ClientBundle, TopPanel.Images, FilePropertiesDialog.Images, MessagePanel.Images, FileList.Images {
153 a57faaf0 Christos Stathis
154 a57faaf0 Christos Stathis
                @Source("gr/grnet/pithos/resources/document.png")
155 a57faaf0 Christos Stathis
                ImageResource folders();
156 a57faaf0 Christos Stathis
157 a57faaf0 Christos Stathis
                @Source("gr/grnet/pithos/resources/edit_group_22.png")
158 a57faaf0 Christos Stathis
                ImageResource groups();
159 a57faaf0 Christos Stathis
160 a57faaf0 Christos Stathis
                @Source("gr/grnet/pithos/resources/search.png")
161 a57faaf0 Christos Stathis
                ImageResource search();
162 a57faaf0 Christos Stathis
        }
163 a57faaf0 Christos Stathis
164 a57faaf0 Christos Stathis
        /**
165 a57faaf0 Christos Stathis
         * The Application Clipboard implementation;
166 a57faaf0 Christos Stathis
         */
167 a57faaf0 Christos Stathis
        private Clipboard clipboard = new Clipboard();
168 a57faaf0 Christos Stathis
169 a57faaf0 Christos Stathis
        /**
170 a57faaf0 Christos Stathis
         * The top panel that contains the menu bar.
171 a57faaf0 Christos Stathis
         */
172 a57faaf0 Christos Stathis
        private TopPanel topPanel;
173 a57faaf0 Christos Stathis
174 a57faaf0 Christos Stathis
        /**
175 a57faaf0 Christos Stathis
         * The panel that contains the various system messages.
176 a57faaf0 Christos Stathis
         */
177 749068ba Christos Stathis
        private MessagePanel messagePanel = new MessagePanel(Pithos.images);
178 a57faaf0 Christos Stathis
179 a57faaf0 Christos Stathis
        /**
180 a57faaf0 Christos Stathis
         * The bottom panel that contains the status bar.
181 a57faaf0 Christos Stathis
         */
182 8e61880b Christos Stathis
        private StatusPanel statusPanel = null;
183 a57faaf0 Christos Stathis
184 a57faaf0 Christos Stathis
        /**
185 a57faaf0 Christos Stathis
         * The file list widget.
186 a57faaf0 Christos Stathis
         */
187 a57faaf0 Christos Stathis
        private FileList fileList;
188 a57faaf0 Christos Stathis
189 a57faaf0 Christos Stathis
        /**
190 a57faaf0 Christos Stathis
         * The tab panel that occupies the right side of the screen.
191 a57faaf0 Christos Stathis
         */
192 a57faaf0 Christos Stathis
        private TabPanel inner = new DecoratedTabPanel(){
193 a57faaf0 Christos Stathis
                
194 63366925 Christos Stathis
//                public void onBrowserEvent(com.google.gwt.user.client.Event event) {
195 63366925 Christos Stathis
//                        if (DOM.eventGetType(event) == Event.ONCONTEXTMENU){
196 63366925 Christos Stathis
//                                if(isFileListShowing()){
197 63366925 Christos Stathis
//                                        getFileList().showContextMenu(event);
198 63366925 Christos Stathis
//                                }
199 63366925 Christos Stathis
//                        }
200 63366925 Christos Stathis
//                };
201 a57faaf0 Christos Stathis
        };
202 a57faaf0 Christos Stathis
203 63366925 Christos Stathis
204 a57faaf0 Christos Stathis
        /**
205 a57faaf0 Christos Stathis
         * The split panel that will contain the left and right panels.
206 a57faaf0 Christos Stathis
         */
207 a57faaf0 Christos Stathis
        private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
208 a57faaf0 Christos Stathis
209 a57faaf0 Christos Stathis
        /**
210 a57faaf0 Christos Stathis
         * The currently selected item in the application, for use by the Edit menu
211 a57faaf0 Christos Stathis
         * commands. Potential types are Folder, File, User and Group.
212 a57faaf0 Christos Stathis
         */
213 a57faaf0 Christos Stathis
        private Object currentSelection;
214 a57faaf0 Christos Stathis
215 a57faaf0 Christos Stathis
216 a57faaf0 Christos Stathis
        /**
217 a57faaf0 Christos Stathis
         * The WebDAV password of the current user
218 a57faaf0 Christos Stathis
         */
219 a57faaf0 Christos Stathis
        private String webDAVPassword;
220 a57faaf0 Christos Stathis
221 a57faaf0 Christos Stathis
        public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
222 a57faaf0 Christos Stathis
223 6084aa02 Christos Stathis
    private String username = null;
224 6084aa02 Christos Stathis
225 6084aa02 Christos Stathis
    /**
226 6084aa02 Christos Stathis
     * The authentication token of the current user.
227 6084aa02 Christos Stathis
     */
228 6084aa02 Christos Stathis
    private String token;
229 6084aa02 Christos Stathis
230 875a0179 Christos Stathis
    private SingleSelectionModel<Folder> folderTreeSelectionModel;
231 875a0179 Christos Stathis
    private FolderTreeViewModel folderTreeViewModel;
232 875a0179 Christos Stathis
    private FolderTreeView folderTreeView;
233 6084aa02 Christos Stathis
234 a6592a15 Christos Stathis
    private SingleSelectionModel<Tag> tagTreeSelectionModel;
235 a6592a15 Christos Stathis
    private TagTreeViewModel tagTreeViewModel;
236 a6592a15 Christos Stathis
    private TagTreeView tagTreeView;
237 a6592a15 Christos Stathis
238 8e61880b Christos Stathis
    private AccountResource account;
239 8e61880b Christos Stathis
240 a57faaf0 Christos Stathis
        @Override
241 a57faaf0 Christos Stathis
        public void onModuleLoad() {
242 6084aa02 Christos Stathis
                if (parseUserCredentials())
243 6084aa02 Christos Stathis
            initialize();
244 6084aa02 Christos Stathis
        }
245 a57faaf0 Christos Stathis
246 6084aa02 Christos Stathis
    private void initialize() {
247 a7c43f26 Christos Stathis
        topPanel = new TopPanel(this, Pithos.images);
248 6084aa02 Christos Stathis
        topPanel.setWidth("100%");
249 6084aa02 Christos Stathis
250 6084aa02 Christos Stathis
        messagePanel.setWidth("100%");
251 6084aa02 Christos Stathis
        messagePanel.setVisible(false);
252 a57faaf0 Christos Stathis
253 6084aa02 Christos Stathis
254 6084aa02 Christos Stathis
        // Inner contains the various lists.
255 6084aa02 Christos Stathis
        inner.sinkEvents(Event.ONCONTEXTMENU);
256 6084aa02 Christos Stathis
        inner.setAnimationEnabled(true);
257 6084aa02 Christos Stathis
        inner.getTabBar().addStyleName("pithos-MainTabBar");
258 6084aa02 Christos Stathis
        inner.getDeckPanel().addStyleName("pithos-MainTabPanelBottom");
259 6084aa02 Christos Stathis
260 6084aa02 Christos Stathis
        inner.setWidth("100%");
261 6084aa02 Christos Stathis
262 6084aa02 Christos Stathis
        inner.addSelectionHandler(new SelectionHandler<Integer>() {
263 6084aa02 Christos Stathis
264 6084aa02 Christos Stathis
            @Override
265 6084aa02 Christos Stathis
            public void onSelection(SelectionEvent<Integer> event) {
266 6084aa02 Christos Stathis
                int tabIndex = event.getSelectedItem();
267 6084aa02 Christos Stathis
                switch (tabIndex) {
268 6084aa02 Christos Stathis
                    case 0:
269 6084aa02 Christos Stathis
                        break;
270 6084aa02 Christos Stathis
                }
271 6084aa02 Christos Stathis
            }
272 6084aa02 Christos Stathis
        });
273 6084aa02 Christos Stathis
274 875a0179 Christos Stathis
        folderTreeSelectionModel = new SingleSelectionModel<Folder>();
275 875a0179 Christos Stathis
        folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
276 875a0179 Christos Stathis
            @Override
277 875a0179 Christos Stathis
            public void onSelectionChange(SelectionChangeEvent event) {
278 a845b044 Christos Stathis
                if (folderTreeSelectionModel.getSelectedObject() != null) {
279 a845b044 Christos Stathis
                    tagTreeSelectionModel.setSelected(tagTreeSelectionModel.getSelectedObject(), false);
280 a845b044 Christos Stathis
                    Folder f = folderTreeSelectionModel.getSelectedObject();
281 5ebc9312 Christos Stathis
                    updateFolder(f, true);
282 a845b044 Christos Stathis
                }
283 875a0179 Christos Stathis
            }
284 875a0179 Christos Stathis
        });
285 875a0179 Christos Stathis
286 a7c43f26 Christos Stathis
        folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
287 875a0179 Christos Stathis
        folderTreeView = new FolderTreeView(folderTreeViewModel);
288 875a0179 Christos Stathis
289 a7c43f26 Christos Stathis
        fileList = new FileList(this, images, folderTreeView);
290 875a0179 Christos Stathis
        inner.add(fileList, createHeaderHTML(AbstractImagePrototype.create(images.folders()), "Files"), true);
291 875a0179 Christos Stathis
292 a6592a15 Christos Stathis
        tagTreeSelectionModel = new SingleSelectionModel<Tag>();
293 0d1e8310 Christos Stathis
        tagTreeSelectionModel.addSelectionChangeHandler(new Handler() {
294 0d1e8310 Christos Stathis
            @Override
295 0d1e8310 Christos Stathis
            public void onSelectionChange(SelectionChangeEvent event) {
296 a845b044 Christos Stathis
                if (tagTreeSelectionModel.getSelectedObject() != null) {
297 a845b044 Christos Stathis
                    folderTreeSelectionModel.setSelected(folderTreeSelectionModel.getSelectedObject(), false);
298 a845b044 Christos Stathis
                    Tag t = tagTreeSelectionModel.getSelectedObject();
299 a845b044 Christos Stathis
                    updateTag(t);
300 a845b044 Christos Stathis
                }
301 0d1e8310 Christos Stathis
            }
302 0d1e8310 Christos Stathis
        });
303 a7c43f26 Christos Stathis
        tagTreeViewModel = new TagTreeViewModel(this, tagTreeSelectionModel);
304 a6592a15 Christos Stathis
        tagTreeView = new TagTreeView(tagTreeViewModel);
305 0d1e8310 Christos Stathis
306 a6592a15 Christos Stathis
        VerticalPanel trees = new VerticalPanel();
307 a6592a15 Christos Stathis
        trees.add(folderTreeView);
308 a6592a15 Christos Stathis
        trees.add(tagTreeView);
309 6084aa02 Christos Stathis
        // Add the left and right panels to the split panel.
310 a6592a15 Christos Stathis
        splitPanel.setLeftWidget(trees);
311 6084aa02 Christos Stathis
        splitPanel.setRightWidget(inner);
312 6084aa02 Christos Stathis
        splitPanel.setSplitPosition("25%");
313 6084aa02 Christos Stathis
        splitPanel.setSize("100%", "100%");
314 6084aa02 Christos Stathis
        splitPanel.addStyleName("pithos-splitPanel");
315 6084aa02 Christos Stathis
316 6084aa02 Christos Stathis
        // Create a dock panel that will contain the menu bar at the top,
317 6084aa02 Christos Stathis
        // the shortcuts to the left, the status bar at the bottom and the
318 6084aa02 Christos Stathis
        // right panel taking the rest.
319 6084aa02 Christos Stathis
        VerticalPanel outer = new VerticalPanel();
320 6084aa02 Christos Stathis
        outer.add(topPanel);
321 6084aa02 Christos Stathis
        outer.add(messagePanel);
322 6084aa02 Christos Stathis
        outer.add(splitPanel);
323 36dc8e18 Christos Stathis
        statusPanel = new StatusPanel();
324 6084aa02 Christos Stathis
        outer.add(statusPanel);
325 6084aa02 Christos Stathis
        outer.setWidth("100%");
326 6084aa02 Christos Stathis
        outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
327 6084aa02 Christos Stathis
328 6084aa02 Christos Stathis
        // Hook the window resize event, so that we can adjust the UI.
329 6084aa02 Christos Stathis
        Window.addResizeHandler(this);
330 6084aa02 Christos Stathis
        // Clear out the window's built-in margin, because we want to take
331 6084aa02 Christos Stathis
        // advantage of the entire client area.
332 6084aa02 Christos Stathis
        Window.setMargin("0px");
333 6084aa02 Christos Stathis
        // Finally, add the outer panel to the RootPanel, so that it will be
334 6084aa02 Christos Stathis
        // displayed.
335 6084aa02 Christos Stathis
        RootPanel.get().add(outer);
336 6084aa02 Christos Stathis
        // Call the window resized handler to get the initial sizes setup. Doing
337 6084aa02 Christos Stathis
        // this in a deferred command causes it to occur after all widgets'
338 6084aa02 Christos Stathis
        // sizes have been computed by the browser.
339 7c818c14 Christos Stathis
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
340 6084aa02 Christos Stathis
341 6084aa02 Christos Stathis
            @Override
342 6084aa02 Christos Stathis
            public void execute() {
343 6084aa02 Christos Stathis
                onWindowResized(Window.getClientHeight());
344 6084aa02 Christos Stathis
            }
345 6084aa02 Christos Stathis
        });
346 7c818c14 Christos Stathis
347 7c818c14 Christos Stathis
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
348 7c818c14 Christos Stathis
            @Override
349 7c818c14 Christos Stathis
            public void execute() {
350 7c818c14 Christos Stathis
                fetchAccount();
351 7c818c14 Christos Stathis
            }
352 7c818c14 Christos Stathis
        });
353 6084aa02 Christos Stathis
    }
354 a57faaf0 Christos Stathis
355 bdda6b2f Christos Stathis
    public void showFiles(Folder f) {
356 875a0179 Christos Stathis
        inner.selectTab(0);
357 05cf9e5b Christos Stathis
        if (f.isTrash()) {
358 05cf9e5b Christos Stathis
            fileList.showTrash();
359 05cf9e5b Christos Stathis
        }
360 05cf9e5b Christos Stathis
        else
361 05cf9e5b Christos Stathis
            fileList.showFiles();
362 07a1b5fe Christos Stathis
        Set<File> files = f.getFiles();
363 a845b044 Christos Stathis
        showFiles(files);
364 a845b044 Christos Stathis
    }
365 a845b044 Christos Stathis
366 a845b044 Christos Stathis
    public void showFiles(Set<File> files) {
367 a845b044 Christos Stathis
        //Iterator<File> iter = files.iterator();
368 a845b044 Christos Stathis
        //fetchFile(iter, files);
369 a845b044 Christos Stathis
        fileList.setFiles(new ArrayList<File>(files));
370 07a1b5fe Christos Stathis
    }
371 07a1b5fe Christos Stathis
372 07a1b5fe Christos Stathis
    private void fetchFile(final Iterator<File> iter, final Set<File> files) {
373 07a1b5fe Christos Stathis
        if (iter.hasNext()) {
374 07a1b5fe Christos Stathis
            File file = iter.next();
375 b51c628b Christos Stathis
            String path = file.getUri() + "?format=json";
376 b51c628b Christos Stathis
            GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
377 07a1b5fe Christos Stathis
                @Override
378 07a1b5fe Christos Stathis
                public void onSuccess(File result) {
379 07a1b5fe Christos Stathis
                    fetchFile(iter, files);
380 07a1b5fe Christos Stathis
                }
381 07a1b5fe Christos Stathis
382 07a1b5fe Christos Stathis
                @Override
383 07a1b5fe Christos Stathis
                public void onError(Throwable t) {
384 07a1b5fe Christos Stathis
                    GWT.log("Error getting file", t);
385 07a1b5fe Christos Stathis
                    if (t instanceof RestException)
386 a7c43f26 Christos Stathis
                        displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
387 07a1b5fe Christos Stathis
                    else
388 a7c43f26 Christos Stathis
                        displayError("System error fetching file: " + t.getMessage());
389 07a1b5fe Christos Stathis
                }
390 07a1b5fe Christos Stathis
            };
391 07a1b5fe Christos Stathis
            getFile.setHeader("X-Auth-Token", "0000");
392 07a1b5fe Christos Stathis
            Scheduler.get().scheduleDeferred(getFile);
393 07a1b5fe Christos Stathis
        }
394 07a1b5fe Christos Stathis
        else
395 07a1b5fe Christos Stathis
            fileList.setFiles(new ArrayList<File>(files));
396 875a0179 Christos Stathis
    }
397 875a0179 Christos Stathis
398 875a0179 Christos Stathis
    /**
399 a57faaf0 Christos Stathis
         * Parse and store the user credentials to the appropriate fields.
400 a57faaf0 Christos Stathis
         */
401 6084aa02 Christos Stathis
        private boolean parseUserCredentials() {
402 36dc8e18 Christos Stathis
        username = Window.Location.getParameter("user");
403 36dc8e18 Christos Stathis
        token = Window.Location.getParameter("token");
404 36dc8e18 Christos Stathis
        Configuration conf = (Configuration) GWT.create(Configuration.class);
405 36dc8e18 Christos Stathis
        if (username == null || username.length() == 0 || token == null || token.length() == 0) {
406 36dc8e18 Christos Stathis
            String cookie = conf.authCookie();
407 36dc8e18 Christos Stathis
            String auth = Cookies.getCookie(cookie);
408 36dc8e18 Christos Stathis
            if (auth == null) {
409 6084aa02 Christos Stathis
                authenticateUser();
410 6084aa02 Christos Stathis
                return false;
411 6084aa02 Christos Stathis
            }
412 6084aa02 Christos Stathis
            else {
413 36dc8e18 Christos Stathis
                String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
414 36dc8e18 Christos Stathis
                if (authSplit.length != 2) {
415 36dc8e18 Christos Stathis
                    authenticateUser();
416 36dc8e18 Christos Stathis
                    return false;
417 36dc8e18 Christos Stathis
                }
418 36dc8e18 Christos Stathis
                else {
419 36dc8e18 Christos Stathis
                    username = authSplit[0];
420 36dc8e18 Christos Stathis
                    token = authSplit[1];
421 36dc8e18 Christos Stathis
                    return true;
422 36dc8e18 Christos Stathis
                }
423 6084aa02 Christos Stathis
            }
424 6084aa02 Christos Stathis
        }
425 36dc8e18 Christos Stathis
        else {
426 36dc8e18 Christos Stathis
            Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token);
427 36dc8e18 Christos Stathis
            return true;
428 36dc8e18 Christos Stathis
        }
429 36dc8e18 Christos Stathis
    }
430 a57faaf0 Christos Stathis
431 6084aa02 Christos Stathis
    /**
432 a57faaf0 Christos Stathis
         * Redirect the user to the login page for authentication.
433 a57faaf0 Christos Stathis
         */
434 a57faaf0 Christos Stathis
        protected void authenticateUser() {
435 6084aa02 Christos Stathis
                Configuration conf = (Configuration) GWT.create(Configuration.class);
436 36dc8e18 Christos Stathis
        Window.Location.assign(Window.Location.getHost() + conf.loginUrl() + "?next=" + Window.Location.getHref());
437 a57faaf0 Christos Stathis
        }
438 a57faaf0 Christos Stathis
439 7c818c14 Christos Stathis
    private void fetchAccount() {
440 b51c628b Christos Stathis
        String path = "?format=json";
441 7c818c14 Christos Stathis
442 b51c628b Christos Stathis
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
443 7c818c14 Christos Stathis
            @Override
444 7c818c14 Christos Stathis
            public void onSuccess(AccountResource result) {
445 7c818c14 Christos Stathis
                account = result;
446 875a0179 Christos Stathis
                inner.selectTab(0);
447 b8822fe7 Christos Stathis
                if (account.getContainers().isEmpty())
448 b8822fe7 Christos Stathis
                    createHomeContainers();
449 b8822fe7 Christos Stathis
                else
450 b8822fe7 Christos Stathis
                    folderTreeViewModel.initialize(account);
451 7c818c14 Christos Stathis
            }
452 7c818c14 Christos Stathis
453 7c818c14 Christos Stathis
            @Override
454 7c818c14 Christos Stathis
            public void onError(Throwable t) {
455 7c818c14 Christos Stathis
                GWT.log("Error getting account", t);
456 7c818c14 Christos Stathis
                if (t instanceof RestException)
457 a7c43f26 Christos Stathis
                    displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
458 7c818c14 Christos Stathis
                else
459 a7c43f26 Christos Stathis
                    displayError("System error fetching user data: " + t.getMessage());
460 7c818c14 Christos Stathis
            }
461 7c818c14 Christos Stathis
        };
462 875a0179 Christos Stathis
        getAccount.setHeader("X-Auth-Token", token);
463 7c818c14 Christos Stathis
        Scheduler.get().scheduleDeferred(getAccount);
464 7c818c14 Christos Stathis
    }
465 7c818c14 Christos Stathis
466 b8822fe7 Christos Stathis
    private void createHomeContainers() {
467 b51c628b Christos Stathis
        String path = "/pithos";
468 b51c628b Christos Stathis
        PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
469 b8822fe7 Christos Stathis
            @Override
470 b8822fe7 Christos Stathis
            public void onSuccess(Resource result) {
471 b8822fe7 Christos Stathis
                fetchAccount();
472 b8822fe7 Christos Stathis
            }
473 b8822fe7 Christos Stathis
474 b8822fe7 Christos Stathis
            @Override
475 b8822fe7 Christos Stathis
            public void onError(Throwable t) {
476 b8822fe7 Christos Stathis
                GWT.log("Error creating pithos", t);
477 b8822fe7 Christos Stathis
                if (t instanceof RestException)
478 a7c43f26 Christos Stathis
                    displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
479 b8822fe7 Christos Stathis
                else
480 a7c43f26 Christos Stathis
                    displayError("System error Error creating pithos: " + t.getMessage());
481 b8822fe7 Christos Stathis
            }
482 b8822fe7 Christos Stathis
        };
483 b8822fe7 Christos Stathis
        createPithos.setHeader("X-Auth-Token", getToken());
484 b8822fe7 Christos Stathis
        Scheduler.get().scheduleDeferred(createPithos);
485 b8822fe7 Christos Stathis
    }
486 b8822fe7 Christos Stathis
487 a57faaf0 Christos Stathis
        /**
488 a57faaf0 Christos Stathis
         * Creates an HTML fragment that places an image & caption together, for use
489 a57faaf0 Christos Stathis
         * in a group header.
490 a57faaf0 Christos Stathis
         *
491 a57faaf0 Christos Stathis
         * @param imageProto an image prototype for an image
492 a57faaf0 Christos Stathis
         * @param caption the group caption
493 a57faaf0 Christos Stathis
         * @return the header HTML fragment
494 a57faaf0 Christos Stathis
         */
495 a57faaf0 Christos Stathis
        private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
496 a57faaf0 Christos Stathis
                String captionHTML = "<table class='caption' cellpadding='0' " 
497 a57faaf0 Christos Stathis
                + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() 
498 a57faaf0 Christos Stathis
                + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'>&nbsp;" 
499 a57faaf0 Christos Stathis
                + caption + "</b></td></tr></table>";
500 a57faaf0 Christos Stathis
                return captionHTML;
501 a57faaf0 Christos Stathis
        }
502 a57faaf0 Christos Stathis
503 a57faaf0 Christos Stathis
        private void onWindowResized(int height) {
504 a57faaf0 Christos Stathis
                // Adjust the split panel to take up the available room in the window.
505 36dc8e18 Christos Stathis
                int newHeight = height - splitPanel.getAbsoluteTop() - 60;
506 a57faaf0 Christos Stathis
                if (newHeight < 1)
507 a57faaf0 Christos Stathis
                        newHeight = 1;
508 a57faaf0 Christos Stathis
                splitPanel.setHeight("" + newHeight);
509 a57faaf0 Christos Stathis
                inner.setHeight("" + newHeight);
510 a57faaf0 Christos Stathis
        }
511 a57faaf0 Christos Stathis
512 a57faaf0 Christos Stathis
        @Override
513 a57faaf0 Christos Stathis
        public void onResize(ResizeEvent event) {
514 a57faaf0 Christos Stathis
                int height = event.getHeight();
515 a57faaf0 Christos Stathis
                onWindowResized(height);
516 a57faaf0 Christos Stathis
        }
517 a57faaf0 Christos Stathis
518 a57faaf0 Christos Stathis
        /**
519 a57faaf0 Christos Stathis
         * Display an error message.
520 a57faaf0 Christos Stathis
         *
521 a57faaf0 Christos Stathis
         * @param msg the message to display
522 a57faaf0 Christos Stathis
         */
523 a57faaf0 Christos Stathis
        public void displayError(String msg) {
524 a57faaf0 Christos Stathis
                messagePanel.displayError(msg);
525 a57faaf0 Christos Stathis
        }
526 a57faaf0 Christos Stathis
527 a57faaf0 Christos Stathis
        /**
528 a57faaf0 Christos Stathis
         * Display a warning message.
529 a57faaf0 Christos Stathis
         *
530 a57faaf0 Christos Stathis
         * @param msg the message to display
531 a57faaf0 Christos Stathis
         */
532 a57faaf0 Christos Stathis
        public void displayWarning(String msg) {
533 a57faaf0 Christos Stathis
                messagePanel.displayWarning(msg);
534 a57faaf0 Christos Stathis
        }
535 a57faaf0 Christos Stathis
536 a57faaf0 Christos Stathis
        /**
537 a57faaf0 Christos Stathis
         * Display an informational message.
538 a57faaf0 Christos Stathis
         *
539 a57faaf0 Christos Stathis
         * @param msg the message to display
540 a57faaf0 Christos Stathis
         */
541 a57faaf0 Christos Stathis
        public void displayInformation(String msg) {
542 a57faaf0 Christos Stathis
                messagePanel.displayInformation(msg);
543 a57faaf0 Christos Stathis
        }
544 a57faaf0 Christos Stathis
545 a57faaf0 Christos Stathis
        /**
546 a57faaf0 Christos Stathis
         * Retrieve the fileList.
547 a57faaf0 Christos Stathis
         *
548 a57faaf0 Christos Stathis
         * @return the fileList
549 a57faaf0 Christos Stathis
         */
550 a57faaf0 Christos Stathis
        public FileList getFileList() {
551 a57faaf0 Christos Stathis
                return fileList;
552 a57faaf0 Christos Stathis
        }
553 a57faaf0 Christos Stathis
554 a57faaf0 Christos Stathis
        /**
555 a57faaf0 Christos Stathis
         * Retrieve the topPanel.
556 a57faaf0 Christos Stathis
         *
557 a57faaf0 Christos Stathis
         * @return the topPanel
558 a57faaf0 Christos Stathis
         */
559 a57faaf0 Christos Stathis
        TopPanel getTopPanel() {
560 a57faaf0 Christos Stathis
                return topPanel;
561 a57faaf0 Christos Stathis
        }
562 a57faaf0 Christos Stathis
563 a57faaf0 Christos Stathis
        /**
564 a57faaf0 Christos Stathis
         * Retrieve the clipboard.
565 a57faaf0 Christos Stathis
         *
566 a57faaf0 Christos Stathis
         * @return the clipboard
567 a57faaf0 Christos Stathis
         */
568 a57faaf0 Christos Stathis
        public Clipboard getClipboard() {
569 a57faaf0 Christos Stathis
                return clipboard;
570 a57faaf0 Christos Stathis
        }
571 a57faaf0 Christos Stathis
572 a57faaf0 Christos Stathis
        public StatusPanel getStatusPanel() {
573 a57faaf0 Christos Stathis
                return statusPanel;
574 a57faaf0 Christos Stathis
        }
575 a57faaf0 Christos Stathis
576 a57faaf0 Christos Stathis
        public String getToken() {
577 a57faaf0 Christos Stathis
                return token;
578 a57faaf0 Christos Stathis
        }
579 a57faaf0 Christos Stathis
580 a57faaf0 Christos Stathis
        public String getWebDAVPassword() {
581 a57faaf0 Christos Stathis
                return webDAVPassword;
582 a57faaf0 Christos Stathis
        }
583 a57faaf0 Christos Stathis
584 a57faaf0 Christos Stathis
        public static native void preventIESelection() /*-{
585 a57faaf0 Christos Stathis
                $doc.body.onselectstart = function () { return false; };
586 a57faaf0 Christos Stathis
        }-*/;
587 a57faaf0 Christos Stathis
588 a57faaf0 Christos Stathis
        public static native void enableIESelection() /*-{
589 a57faaf0 Christos Stathis
                if ($doc.body.onselectstart != null)
590 a57faaf0 Christos Stathis
                $doc.body.onselectstart = null;
591 a57faaf0 Christos Stathis
        }-*/;
592 a57faaf0 Christos Stathis
593 a57faaf0 Christos Stathis
        /**
594 a57faaf0 Christos Stathis
         * @return the absolute path of the API root URL
595 a57faaf0 Christos Stathis
         */
596 a57faaf0 Christos Stathis
        public String getApiPath() {
597 a57faaf0 Christos Stathis
                Configuration conf = (Configuration) GWT.create(Configuration.class);
598 6084aa02 Christos Stathis
                return conf.apiPath();
599 a57faaf0 Christos Stathis
        }
600 a57faaf0 Christos Stathis
601 a57faaf0 Christos Stathis
        /**
602 a57faaf0 Christos Stathis
         * History support for folder navigation
603 a57faaf0 Christos Stathis
         * adds a new browser history entry
604 a57faaf0 Christos Stathis
         *
605 a57faaf0 Christos Stathis
         * @param key
606 a57faaf0 Christos Stathis
         */
607 a57faaf0 Christos Stathis
        public void updateHistory(String key){
608 a57faaf0 Christos Stathis
//                Replace any whitespace of the initial string to "+"
609 a57faaf0 Christos Stathis
//                String result = key.replaceAll("\\s","+");
610 a57faaf0 Christos Stathis
//                Add a new browser history entry.
611 a57faaf0 Christos Stathis
//                History.newItem(result);
612 a57faaf0 Christos Stathis
                History.newItem(key);
613 a57faaf0 Christos Stathis
        }
614 a57faaf0 Christos Stathis
615 602ebeb4 Christos Stathis
    public void deleteFolder(final Folder folder) {
616 602ebeb4 Christos Stathis
        String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + folder.getPrefix();
617 602ebeb4 Christos Stathis
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
618 602ebeb4 Christos Stathis
        builder.setHeader("If-Modified-Since", "0");
619 602ebeb4 Christos Stathis
        builder.setHeader("X-Auth-Token", getToken());
620 602ebeb4 Christos Stathis
        try {
621 602ebeb4 Christos Stathis
            builder.sendRequest("", new RequestCallback() {
622 602ebeb4 Christos Stathis
                @Override
623 602ebeb4 Christos Stathis
                public void onResponseReceived(Request request, Response response) {
624 602ebeb4 Christos Stathis
                    if (response.getStatusCode() == Response.SC_OK) {
625 602ebeb4 Christos Stathis
                        JSONValue json = JSONParser.parseStrict(response.getText());
626 602ebeb4 Christos Stathis
                        JSONArray array = json.isArray();
627 602ebeb4 Christos Stathis
                        int i = 0;
628 602ebeb4 Christos Stathis
                        if (array != null) {
629 602ebeb4 Christos Stathis
                            deleteObject(folder, i, array);
630 602ebeb4 Christos Stathis
                        }
631 602ebeb4 Christos Stathis
                    }
632 602ebeb4 Christos Stathis
                }
633 602ebeb4 Christos Stathis
634 602ebeb4 Christos Stathis
                @Override
635 602ebeb4 Christos Stathis
                public void onError(Request request, Throwable exception) {
636 a7c43f26 Christos Stathis
                    displayError("System error unable to delete folder: " + exception.getMessage());
637 602ebeb4 Christos Stathis
                }
638 602ebeb4 Christos Stathis
            });
639 602ebeb4 Christos Stathis
        }
640 602ebeb4 Christos Stathis
        catch (RequestException e) {
641 602ebeb4 Christos Stathis
        }
642 602ebeb4 Christos Stathis
    }
643 602ebeb4 Christos Stathis
644 602ebeb4 Christos Stathis
    public void deleteObject(final Folder folder, final int i, final JSONArray array) {
645 602ebeb4 Christos Stathis
        if (i < array.size()) {
646 602ebeb4 Christos Stathis
            JSONObject o = array.get(i).isObject();
647 602ebeb4 Christos Stathis
            if (o != null && !o.containsKey("subdir")) {
648 602ebeb4 Christos Stathis
                JSONString name = o.get("name").isString();
649 b51c628b Christos Stathis
                String path = "/" + folder.getContainer() + "/" + name.stringValue();
650 b51c628b Christos Stathis
                DeleteRequest delete = new DeleteRequest(getApiPath(), getUsername(), path) {
651 602ebeb4 Christos Stathis
                    @Override
652 602ebeb4 Christos Stathis
                    public void onSuccess(Resource result) {
653 602ebeb4 Christos Stathis
                        deleteObject(folder, i + 1, array);
654 602ebeb4 Christos Stathis
                    }
655 602ebeb4 Christos Stathis
656 602ebeb4 Christos Stathis
                    @Override
657 602ebeb4 Christos Stathis
                    public void onError(Throwable t) {
658 602ebeb4 Christos Stathis
                        GWT.log("", t);
659 a7c43f26 Christos Stathis
                        displayError("System error unable to delete folder: " + t.getMessage());
660 602ebeb4 Christos Stathis
                    }
661 602ebeb4 Christos Stathis
                };
662 602ebeb4 Christos Stathis
                delete.setHeader("X-Auth-Token", getToken());
663 602ebeb4 Christos Stathis
                Scheduler.get().scheduleDeferred(delete);
664 602ebeb4 Christos Stathis
            }
665 602ebeb4 Christos Stathis
            else {
666 602ebeb4 Christos Stathis
                String subdir = o.get("subdir").isString().stringValue();
667 602ebeb4 Christos Stathis
                subdir = subdir.substring(0, subdir.length() - 1);
668 602ebeb4 Christos Stathis
                String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + subdir;
669 602ebeb4 Christos Stathis
                RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
670 602ebeb4 Christos Stathis
                builder.setHeader("If-Modified-Since", "0");
671 602ebeb4 Christos Stathis
                builder.setHeader("X-Auth-Token", getToken());
672 602ebeb4 Christos Stathis
                try {
673 602ebeb4 Christos Stathis
                    builder.sendRequest("", new RequestCallback() {
674 602ebeb4 Christos Stathis
                        @Override
675 602ebeb4 Christos Stathis
                        public void onResponseReceived(Request request, Response response) {
676 602ebeb4 Christos Stathis
                            if (response.getStatusCode() == Response.SC_OK) {
677 602ebeb4 Christos Stathis
                                JSONValue json = JSONParser.parseStrict(response.getText());
678 602ebeb4 Christos Stathis
                                JSONArray array2 = json.isArray();
679 602ebeb4 Christos Stathis
                                if (array2 != null) {
680 602ebeb4 Christos Stathis
                                    int l = array.size();
681 602ebeb4 Christos Stathis
                                    for (int j=0; j<array2.size(); j++) {
682 602ebeb4 Christos Stathis
                                        array.set(l++, array2.get(j));
683 602ebeb4 Christos Stathis
                                    }
684 602ebeb4 Christos Stathis
                                }
685 602ebeb4 Christos Stathis
                                deleteObject(folder, i + 1, array);
686 602ebeb4 Christos Stathis
                            }
687 602ebeb4 Christos Stathis
                        }
688 602ebeb4 Christos Stathis
689 602ebeb4 Christos Stathis
                        @Override
690 602ebeb4 Christos Stathis
                        public void onError(Request request, Throwable exception) {
691 a7c43f26 Christos Stathis
                            displayError("System error unable to delete folder: " + exception.getMessage());
692 602ebeb4 Christos Stathis
                        }
693 602ebeb4 Christos Stathis
                    });
694 602ebeb4 Christos Stathis
                }
695 602ebeb4 Christos Stathis
                catch (RequestException e) {
696 602ebeb4 Christos Stathis
                }
697 602ebeb4 Christos Stathis
            }
698 602ebeb4 Christos Stathis
        }
699 602ebeb4 Christos Stathis
        else {
700 b51c628b Christos Stathis
            String path = folder.getUri();
701 b51c628b Christos Stathis
            DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
702 602ebeb4 Christos Stathis
                @Override
703 602ebeb4 Christos Stathis
                public void onSuccess(Resource result) {
704 5ebc9312 Christos Stathis
                    updateFolder(folder.getParent(), true);
705 602ebeb4 Christos Stathis
                }
706 602ebeb4 Christos Stathis
707 602ebeb4 Christos Stathis
                @Override
708 602ebeb4 Christos Stathis
                public void onError(Throwable t) {
709 602ebeb4 Christos Stathis
                    GWT.log("", t);
710 602ebeb4 Christos Stathis
                    if (t instanceof RestException) {
711 602ebeb4 Christos Stathis
                        displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
712 602ebeb4 Christos Stathis
                    }
713 602ebeb4 Christos Stathis
                    else
714 a7c43f26 Christos Stathis
                        displayError("System error unable to delete folder: " + t.getMessage());
715 602ebeb4 Christos Stathis
                }
716 602ebeb4 Christos Stathis
            };
717 602ebeb4 Christos Stathis
            deleteFolder.setHeader("X-Auth-Token", getToken());
718 602ebeb4 Christos Stathis
            Scheduler.get().scheduleDeferred(deleteFolder);
719 602ebeb4 Christos Stathis
        }
720 602ebeb4 Christos Stathis
    }
721 fad48f0d Christos Stathis
722 fad48f0d Christos Stathis
    public FolderTreeView getFolderTreeView() {
723 fad48f0d Christos Stathis
        return folderTreeView;
724 fad48f0d Christos Stathis
    }
725 d6be39da Christos Stathis
726 d6be39da Christos Stathis
    public void copyFiles(final Iterator<File> iter, final String targetUri, final Command callback) {
727 d6be39da Christos Stathis
        if (iter.hasNext()) {
728 d6be39da Christos Stathis
            File file = iter.next();
729 b51c628b Christos Stathis
            String path = targetUri + "/" + file.getName();
730 b51c628b Christos Stathis
            PutRequest copyFile = new PutRequest(getApiPath(), getUsername(), path) {
731 d6be39da Christos Stathis
                @Override
732 d6be39da Christos Stathis
                public void onSuccess(Resource result) {
733 d6be39da Christos Stathis
                    copyFiles(iter, targetUri, callback);
734 d6be39da Christos Stathis
                }
735 d6be39da Christos Stathis
736 d6be39da Christos Stathis
                @Override
737 d6be39da Christos Stathis
                public void onError(Throwable t) {
738 d6be39da Christos Stathis
                    GWT.log("", t);
739 d6be39da Christos Stathis
                    if (t instanceof RestException) {
740 a7c43f26 Christos Stathis
                        displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
741 d6be39da Christos Stathis
                    }
742 d6be39da Christos Stathis
                    else
743 a7c43f26 Christos Stathis
                        displayError("System error unable to copy file: "+t.getMessage());
744 d6be39da Christos Stathis
                }
745 d6be39da Christos Stathis
            };
746 d6be39da Christos Stathis
            copyFile.setHeader("X-Auth-Token", getToken());
747 d6be39da Christos Stathis
            copyFile.setHeader("X-Copy-From", file.getUri());
748 d6be39da Christos Stathis
            Scheduler.get().scheduleDeferred(copyFile);
749 d6be39da Christos Stathis
        }
750 d6be39da Christos Stathis
        else  if (callback != null) {
751 d6be39da Christos Stathis
            callback.execute();
752 d6be39da Christos Stathis
        }
753 d6be39da Christos Stathis
    }
754 d6be39da Christos Stathis
755 d6be39da Christos Stathis
    public void copySubfolders(final Iterator<Folder> iter, final String targetUri, final Command callback) {
756 d6be39da Christos Stathis
        if (iter.hasNext()) {
757 d6be39da Christos Stathis
            final Folder f = iter.next();
758 d6be39da Christos Stathis
            copyFolder(f, targetUri, callback);
759 d6be39da Christos Stathis
        }
760 d6be39da Christos Stathis
        else  if (callback != null) {
761 d6be39da Christos Stathis
            callback.execute();
762 d6be39da Christos Stathis
        }
763 d6be39da Christos Stathis
    }
764 d6be39da Christos Stathis
765 d6be39da Christos Stathis
    public void copyFolder(final Folder f, final String targetUri, final Command callback) {
766 b51c628b Christos Stathis
        String path = targetUri + "/" + f.getName();
767 b51c628b Christos Stathis
        PutRequest createFolder = new PutRequest(getApiPath(), getUsername(), path) {
768 d6be39da Christos Stathis
            @Override
769 d6be39da Christos Stathis
            public void onSuccess(Resource result) {
770 d6be39da Christos Stathis
                Iterator<File> iter = f.getFiles().iterator();
771 d6be39da Christos Stathis
                copyFiles(iter, targetUri + "/" + f.getName(), new Command() {
772 d6be39da Christos Stathis
                    @Override
773 d6be39da Christos Stathis
                    public void execute() {
774 d6be39da Christos Stathis
                        Iterator<Folder> iterf = f.getSubfolders().iterator();
775 d6be39da Christos Stathis
                        copySubfolders(iterf, targetUri + "/" + f.getName(), new Command() {
776 d6be39da Christos Stathis
                            @Override
777 d6be39da Christos Stathis
                            public void execute() {
778 d6be39da Christos Stathis
                                callback.execute();
779 d6be39da Christos Stathis
                            }
780 d6be39da Christos Stathis
                        });
781 d6be39da Christos Stathis
                    }
782 d6be39da Christos Stathis
                });
783 d6be39da Christos Stathis
            }
784 d6be39da Christos Stathis
785 d6be39da Christos Stathis
            @Override
786 d6be39da Christos Stathis
            public void onError(Throwable t) {
787 d6be39da Christos Stathis
                GWT.log("", t);
788 d6be39da Christos Stathis
                if (t instanceof RestException) {
789 d6be39da Christos Stathis
                    displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
790 d6be39da Christos Stathis
                }
791 d6be39da Christos Stathis
                else
792 d6be39da Christos Stathis
                    displayError("System error creating folder:" + t.getMessage());
793 d6be39da Christos Stathis
            }
794 d6be39da Christos Stathis
        };
795 d6be39da Christos Stathis
        createFolder.setHeader("X-Auth-Token", getToken());
796 d6be39da Christos Stathis
        createFolder.setHeader("Accept", "*/*");
797 d6be39da Christos Stathis
        createFolder.setHeader("Content-Length", "0");
798 d6be39da Christos Stathis
        createFolder.setHeader("Content-Type", "application/folder");
799 d6be39da Christos Stathis
        Scheduler.get().scheduleDeferred(createFolder);
800 d6be39da Christos Stathis
    }
801 a57faaf0 Christos Stathis
}