Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / othersharedtree / OtherSharedTreeViewModel.java @ a2f617f8

History | View | Annotate | Download (19 kB)

1 6d8e0f58 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 6d8e0f58 Christos Stathis
 *
4 6d8e0f58 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 6d8e0f58 Christos Stathis
 * without modification, are permitted provided that the following
6 6d8e0f58 Christos Stathis
 * conditions are met:
7 6d8e0f58 Christos Stathis
 *
8 6d8e0f58 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 6d8e0f58 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 6d8e0f58 Christos Stathis
 *      disclaimer.
11 6d8e0f58 Christos Stathis
 *
12 6d8e0f58 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 6d8e0f58 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 6d8e0f58 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 6d8e0f58 Christos Stathis
 *      provided with the distribution.
16 6d8e0f58 Christos Stathis
 *
17 6d8e0f58 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 6d8e0f58 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 6d8e0f58 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 6d8e0f58 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 6d8e0f58 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 6d8e0f58 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 6d8e0f58 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 6d8e0f58 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 6d8e0f58 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 6d8e0f58 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 6d8e0f58 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 6d8e0f58 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 6d8e0f58 Christos Stathis
 *
30 6d8e0f58 Christos Stathis
 * The views and conclusions contained in the software and
31 6d8e0f58 Christos Stathis
 * documentation are those of the authors and should not be
32 6d8e0f58 Christos Stathis
 * interpreted as representing official policies, either expressed
33 6d8e0f58 Christos Stathis
 * or implied, of GRNET S.A.
34 6d8e0f58 Christos Stathis
 */
35 6d8e0f58 Christos Stathis
36 6d8e0f58 Christos Stathis
package gr.grnet.pithos.web.client.othersharedtree;
37 6d8e0f58 Christos Stathis
38 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.FolderContextMenu;
39 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
40 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.SharingUsers;
41 220831bd Christos Stathis
import gr.grnet.pithos.web.client.foldertree.AccountResource;
42 220831bd Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
43 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
44 a2f617f8 Christos Stathis
import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView;
45 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView.Templates;
46 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
47 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
48 6d8e0f58 Christos Stathis
49 6d8e0f58 Christos Stathis
import java.util.HashMap;
50 220831bd Christos Stathis
import java.util.HashSet;
51 6d8e0f58 Christos Stathis
import java.util.Iterator;
52 6d8e0f58 Christos Stathis
import java.util.Map;
53 220831bd Christos Stathis
import java.util.Set;
54 6d8e0f58 Christos Stathis
55 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
56 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.Cell;
57 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.TextCell;
58 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.ValueUpdater;
59 6d8e0f58 Christos Stathis
import com.google.gwt.core.client.GWT;
60 6d8e0f58 Christos Stathis
import com.google.gwt.core.client.Scheduler;
61 6d8e0f58 Christos Stathis
import com.google.gwt.event.dom.client.ContextMenuEvent;
62 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
63 f5023f13 Christos Stathis
import com.google.gwt.http.client.URL;
64 6d8e0f58 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtml;
65 6d8e0f58 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
66 6d8e0f58 Christos Stathis
import com.google.gwt.text.shared.SafeHtmlRenderer;
67 6d8e0f58 Christos Stathis
import com.google.gwt.user.client.Command;
68 6d8e0f58 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
69 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
70 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
71 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
72 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
73 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
74 6d8e0f58 Christos Stathis
75 6d8e0f58 Christos Stathis
public class OtherSharedTreeViewModel implements TreeViewModel {
76 6d8e0f58 Christos Stathis
77 7c54d867 Christos Stathis
        private static final String treeTitle = "Other 's shared";
78 6d8e0f58 Christos Stathis
    protected Pithos app;
79 6d8e0f58 Christos Stathis
80 6d8e0f58 Christos Stathis
    private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) {
81 6d8e0f58 Christos Stathis
82 6d8e0f58 Christos Stathis
       @Override
83 ebead1b5 Christos Stathis
        public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
84 6d8e0f58 Christos Stathis
            String html = AbstractImagePrototype.create(OtherSharedTreeView.images.folderYellow()).getHTML();
85 a7b6e464 Christos Stathis
            safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
86 6d8e0f58 Christos Stathis
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
87 6d8e0f58 Christos Stathis
        }
88 6d8e0f58 Christos Stathis
89 6d8e0f58 Christos Stathis
        @Override
90 a2f617f8 Christos Stathis
        public void onBrowserEvent(Context context, com.google.gwt.dom.client.Element parent, final Folder folder, com.google.gwt.dom.client.NativeEvent event, ValueUpdater<Folder> valueUpdater) {
91 6d8e0f58 Christos Stathis
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
92 a2f617f8 Christos Stathis
                    final int x = event.getClientX();
93 a2f617f8 Christos Stathis
                    final int y = event.getClientY();
94 6d8e0f58 Christos Stathis
                OtherSharedTreeViewModel.this.selectionModel.setSelected(folder, true);
95 a2f617f8 Christos Stathis
                app.scheduleFolderHeadCommand(folder, new Command() {
96 a2f617f8 Christos Stathis
                                        
97 a2f617f8 Christos Stathis
                                        @Override
98 a2f617f8 Christos Stathis
                                        public void execute() {
99 a2f617f8 Christos Stathis
                                FolderContextMenu menu = new FolderContextMenu(app, OtherSharedTreeView.images, app.getSelectedTree(), folder);
100 a2f617f8 Christos Stathis
                                menu.setPopupPosition(x, y);
101 a2f617f8 Christos Stathis
                                menu.show();
102 a2f617f8 Christos Stathis
                                        }
103 a2f617f8 Christos Stathis
                                });
104 6d8e0f58 Christos Stathis
            }
105 6d8e0f58 Christos Stathis
        }
106 6d8e0f58 Christos Stathis
    };
107 6d8e0f58 Christos Stathis
108 6d8e0f58 Christos Stathis
    private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
109 220831bd Christos Stathis
    protected ListDataProvider<String> userLevelDataProvider = new ListDataProvider<String>();
110 6d8e0f58 Christos Stathis
111 220831bd Christos Stathis
    protected Map<String, ListDataProvider<Folder>> userDataProviderMap = new HashMap<String, ListDataProvider<Folder>>();
112 6d8e0f58 Christos Stathis
    private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>();
113 220831bd Christos Stathis
    
114 220831bd Christos Stathis
    protected Map<String, Set<File>> sharedFiles = new HashMap<String, Set<File>>();
115 6d8e0f58 Christos Stathis
116 220831bd Christos Stathis
    protected SingleSelectionModel<Folder> selectionModel;
117 6d8e0f58 Christos Stathis
118 6d8e0f58 Christos Stathis
    public OtherSharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
119 6d8e0f58 Christos Stathis
        app = _app;
120 6d8e0f58 Christos Stathis
        this.selectionModel = selectionModel;
121 6d8e0f58 Christos Stathis
    }
122 6d8e0f58 Christos Stathis
123 6d8e0f58 Christos Stathis
    @Override
124 6d8e0f58 Christos Stathis
    public <T> NodeInfo<?> getNodeInfo(T value) {
125 6d8e0f58 Christos Stathis
        if (value == null) {
126 7c54d867 Christos Stathis
            rootDataProvider.getList().add(treeTitle);
127 6d8e0f58 Christos Stathis
            final SingleSelectionModel<String> selectionModel2 = new SingleSelectionModel<String>();
128 6d8e0f58 Christos Stathis
            selectionModel2.addSelectionChangeHandler(new Handler() {
129 6d8e0f58 Christos Stathis
130 6d8e0f58 Christos Stathis
                @Override
131 ebead1b5 Christos Stathis
                public void onSelectionChange(SelectionChangeEvent event) {
132 6d8e0f58 Christos Stathis
                    if (selectionModel2.getSelectedObject() != null) {
133 f1b24b82 Christos Stathis
                            app.deselectOthers(app.getOtherSharedTreeView(), selectionModel2);
134 3ebb88f4 Christos Stathis
                            app.applyPermissions(null);
135 220831bd Christos Stathis
                            app.showFiles(new HashSet<File>());
136 6d8e0f58 Christos Stathis
                    }
137 b75210b5 Christos Stathis
                    app.showRelevantToolbarButtons();
138 6d8e0f58 Christos Stathis
                }
139 6d8e0f58 Christos Stathis
            });
140 6d8e0f58 Christos Stathis
            app.addSelectionModel(selectionModel2);
141 6d8e0f58 Christos Stathis
            return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
142 6d8e0f58 Christos Stathis
                @Override
143 6d8e0f58 Christos Stathis
                public SafeHtml render(String object) {
144 6d8e0f58 Christos Stathis
                    SafeHtmlBuilder builder = new SafeHtmlBuilder();
145 6d8e0f58 Christos Stathis
                    render(object, builder);
146 6d8e0f58 Christos Stathis
                    return builder.toSafeHtml();
147 6d8e0f58 Christos Stathis
                }
148 6d8e0f58 Christos Stathis
149 6d8e0f58 Christos Stathis
                @Override
150 6d8e0f58 Christos Stathis
                public void render(String object, SafeHtmlBuilder builder) {
151 6d8e0f58 Christos Stathis
                    String html = AbstractImagePrototype.create(OtherSharedTreeView.images.othersShared()).getHTML();
152 a7b6e464 Christos Stathis
                    builder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
153 6d8e0f58 Christos Stathis
                    builder.append(OtherSharedTreeView.Templates.INSTANCE.nameSpan(object));
154 6d8e0f58 Christos Stathis
                }
155 6d8e0f58 Christos Stathis
            }),  selectionModel2, null);
156 6d8e0f58 Christos Stathis
        }
157 6d8e0f58 Christos Stathis
        else if (value instanceof String) {
158 7c54d867 Christos Stathis
                if (value.equals(treeTitle)) {
159 7c54d867 Christos Stathis
                        fetchSharingUsers(null);
160 6d8e0f58 Christos Stathis
                    final SingleSelectionModel<String> selectionModel3 = new SingleSelectionModel<String>();
161 6d8e0f58 Christos Stathis
                    selectionModel3.addSelectionChangeHandler(new Handler() {
162 6d8e0f58 Christos Stathis
163 6d8e0f58 Christos Stathis
                        @Override
164 ebead1b5 Christos Stathis
                        public void onSelectionChange(SelectionChangeEvent event) {
165 6d8e0f58 Christos Stathis
                            if (selectionModel3.getSelectedObject() != null) {
166 f1b24b82 Christos Stathis
                                    app.deselectOthers(app.getOtherSharedTreeView(), selectionModel3);
167 3ebb88f4 Christos Stathis
                                    app.applyPermissions(null);
168 220831bd Christos Stathis
                                    String username = selectionModel3.getSelectedObject();
169 63ee965c Christos Stathis
                                            if (userDataProviderMap.get(username) == null) {
170 63ee965c Christos Stathis
                                                    userDataProviderMap.put(username, new ListDataProvider<Folder>());
171 63ee965c Christos Stathis
                                            }
172 220831bd Christos Stathis
                                    fetchSharedFiles(username, userDataProviderMap.get(username));
173 6d8e0f58 Christos Stathis
                            }
174 b75210b5 Christos Stathis
                            app.showRelevantToolbarButtons();
175 6d8e0f58 Christos Stathis
                        }
176 6d8e0f58 Christos Stathis
                    });
177 6d8e0f58 Christos Stathis
                    app.addSelectionModel(selectionModel3);
178 220831bd Christos Stathis
                    return new DefaultNodeInfo<String>(userLevelDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
179 6d8e0f58 Christos Stathis
180 6d8e0f58 Christos Stathis
                                        @Override
181 6d8e0f58 Christos Stathis
                                        public SafeHtml render(String object) {
182 6d8e0f58 Christos Stathis
                            SafeHtmlBuilder builder = new SafeHtmlBuilder();
183 6d8e0f58 Christos Stathis
                            render(object, builder);
184 6d8e0f58 Christos Stathis
                            return builder.toSafeHtml();
185 6d8e0f58 Christos Stathis
                                        }
186 6d8e0f58 Christos Stathis
187 6d8e0f58 Christos Stathis
                                        @Override
188 6d8e0f58 Christos Stathis
                                        public void render(String object, SafeHtmlBuilder builder) {
189 220831bd Christos Stathis
                            String html = AbstractImagePrototype.create(OtherSharedTreeView.images.user()).getHTML();
190 a7b6e464 Christos Stathis
                            builder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
191 6d8e0f58 Christos Stathis
                            builder.append(OtherSharedTreeView.Templates.INSTANCE.nameSpan(object));
192 6d8e0f58 Christos Stathis
                                        }
193 6d8e0f58 Christos Stathis
                                }), selectionModel3, null);
194 6d8e0f58 Christos Stathis
                }
195 220831bd Christos Stathis
                        String username = (String) value;
196 220831bd Christos Stathis
                        if (userDataProviderMap.get(username) == null) {
197 220831bd Christos Stathis
                                userDataProviderMap.put(username, new ListDataProvider<Folder>());
198 220831bd Christos Stathis
                        }
199 220831bd Christos Stathis
                        final ListDataProvider<Folder> dataProvider = userDataProviderMap.get(username);
200 7c54d867 Christos Stathis
                        fetchSharedContainers(username, dataProvider, null);
201 220831bd Christos Stathis
                        return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
202 6d8e0f58 Christos Stathis
        }
203 6d8e0f58 Christos Stathis
        else {
204 6d8e0f58 Christos Stathis
            final Folder f = (Folder) value;
205 6d8e0f58 Christos Stathis
            if (dataProviderMap.get(f) == null) {
206 6d8e0f58 Christos Stathis
                dataProviderMap.put(f, new ListDataProvider<Folder>());
207 6d8e0f58 Christos Stathis
            }
208 6d8e0f58 Christos Stathis
            final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
209 6d8e0f58 Christos Stathis
            fetchFolder(f, dataProvider, false);
210 6d8e0f58 Christos Stathis
            return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
211 6d8e0f58 Christos Stathis
        }
212 6d8e0f58 Christos Stathis
    }
213 6d8e0f58 Christos Stathis
214 7c54d867 Christos Stathis
    private void fetchSharingUsers(final Command callback) {
215 6d8e0f58 Christos Stathis
        GetRequest<SharingUsers> getSharingUsers = new GetRequest<SharingUsers>(SharingUsers.class, app.getApiPath(), "", "?format=json") {
216 6d8e0f58 Christos Stathis
            @Override
217 7811b9d1 Christos Stathis
            public void onSuccess(final SharingUsers _result) {
218 220831bd Christos Stathis
                userLevelDataProvider.getList().clear();
219 7811b9d1 Christos Stathis
                userLevelDataProvider.getList().addAll(_result.getUsers());
220 7811b9d1 Christos Stathis
                for (String name : _result.getUsers()) {
221 220831bd Christos Stathis
                        sharedFiles.put(name, new HashSet<File>());
222 220831bd Christos Stathis
                }
223 7c54d867 Christos Stathis
                Iterator<String> iter = _result.getUsers().iterator();
224 7c54d867 Christos Stathis
                fetchSharedContainers(iter, callback);
225 6d8e0f58 Christos Stathis
            }
226 6d8e0f58 Christos Stathis
227 6d8e0f58 Christos Stathis
            @Override
228 6d8e0f58 Christos Stathis
            public void onError(Throwable t) {
229 6d8e0f58 Christos Stathis
                GWT.log("Error getting folder", t);
230 3f8622d4 Christos Stathis
                                app.setError(t);
231 6d8e0f58 Christos Stathis
                if (t instanceof RestException)
232 6d8e0f58 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
233 6d8e0f58 Christos Stathis
                else
234 6d8e0f58 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
235 6d8e0f58 Christos Stathis
            }
236 9539e23d Christos Stathis
237 9539e23d Christos Stathis
                        @Override
238 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
239 9539e23d Christos Stathis
                                app.sessionExpired();
240 9539e23d Christos Stathis
                        }
241 6d8e0f58 Christos Stathis
        };
242 6d8e0f58 Christos Stathis
        getSharingUsers.setHeader("X-Auth-Token", app.getToken());
243 6d8e0f58 Christos Stathis
        Scheduler.get().scheduleDeferred(getSharingUsers);
244 6d8e0f58 Christos Stathis
        }
245 6d8e0f58 Christos Stathis
246 7c54d867 Christos Stathis
        protected void fetchSharedContainers(final Iterator<String> iter, final Command callback) {
247 7c54d867 Christos Stathis
                if (iter.hasNext()) {
248 7c54d867 Christos Stathis
                        String username = iter.next();
249 7c54d867 Christos Stathis
                        if (userDataProviderMap.get(username) == null) {
250 7c54d867 Christos Stathis
                                userDataProviderMap.put(username, new ListDataProvider<Folder>());
251 7c54d867 Christos Stathis
                        }
252 7c54d867 Christos Stathis
                        final ListDataProvider<Folder> dataProvider = userDataProviderMap.get(username);
253 7c54d867 Christos Stathis
                        fetchSharedContainers(username, dataProvider, new Command() {
254 7c54d867 Christos Stathis
                                
255 7c54d867 Christos Stathis
                                @Override
256 7c54d867 Christos Stathis
                                public void execute() {
257 7c54d867 Christos Stathis
                                        fetchSharedContainers(iter, callback);
258 7c54d867 Christos Stathis
                                        
259 7c54d867 Christos Stathis
                                }
260 7c54d867 Christos Stathis
                        });
261 7c54d867 Christos Stathis
                }
262 7c54d867 Christos Stathis
                else
263 7c54d867 Christos Stathis
                        if (callback != null)
264 7c54d867 Christos Stathis
                                callback.execute();
265 7c54d867 Christos Stathis
        }
266 7c54d867 Christos Stathis
267 6d8e0f58 Christos Stathis
        @Override
268 6d8e0f58 Christos Stathis
    public boolean isLeaf(Object o) {
269 7c54d867 Christos Stathis
                if (o == null)
270 7c54d867 Christos Stathis
                        return false;
271 7c54d867 Christos Stathis
                else if (o instanceof Folder) {
272 6d8e0f58 Christos Stathis
            Folder f = (Folder) o;
273 6d8e0f58 Christos Stathis
            return f.getSubfolders().isEmpty();
274 6d8e0f58 Christos Stathis
        }
275 7c54d867 Christos Stathis
                else {
276 7c54d867 Christos Stathis
                        if (o.equals(treeTitle))
277 7c54d867 Christos Stathis
                                return userLevelDataProvider.getList().isEmpty();
278 7c54d867 Christos Stathis
                        ListDataProvider<Folder> dp = userDataProviderMap.get(o);
279 7c54d867 Christos Stathis
                        if (dp != null)
280 7c54d867 Christos Stathis
                                return dp.getList().isEmpty();
281 7c54d867 Christos Stathis
                        return true;
282 7c54d867 Christos Stathis
                }
283 6d8e0f58 Christos Stathis
    }
284 6d8e0f58 Christos Stathis
285 7c54d867 Christos Stathis
        private void fetchSharedContainers(final String username, final ListDataProvider<Folder> dataProvider, final Command callback) {
286 220831bd Christos Stathis
                GetRequest<AccountResource> getUserSharedContainers = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), username, "?format=json") {
287 220831bd Christos Stathis
288 220831bd Christos Stathis
                        @Override
289 7811b9d1 Christos Stathis
                        public void onSuccess(AccountResource _result) {
290 220831bd Christos Stathis
                            final ListDataProvider<Folder> tempProvider = new ListDataProvider<Folder>();
291 7811b9d1 Christos Stathis
                                Iterator<Folder> iter = _result.getContainers().iterator();
292 220831bd Christos Stathis
                                fetchFolder(username, iter, tempProvider, new Command() {
293 220831bd Christos Stathis
                                        
294 220831bd Christos Stathis
                                        @Override
295 220831bd Christos Stathis
                                        public void execute() {
296 220831bd Christos Stathis
                                                dataProvider.getList().clear();
297 220831bd Christos Stathis
                                                dataProvider.getList().addAll(tempProvider.getList());
298 7c54d867 Christos Stathis
                                                if (callback != null)
299 7c54d867 Christos Stathis
                                                        callback.execute();
300 220831bd Christos Stathis
                                        }
301 220831bd Christos Stathis
                                });
302 220831bd Christos Stathis
                        }
303 220831bd Christos Stathis
304 220831bd Christos Stathis
                        @Override
305 220831bd Christos Stathis
                        public void onError(Throwable t) {
306 220831bd Christos Stathis
                GWT.log("Error getting account", t);
307 3f8622d4 Christos Stathis
                                app.setError(t);
308 220831bd Christos Stathis
                if (t instanceof RestException)
309 220831bd Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
310 220831bd Christos Stathis
                else
311 220831bd Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
312 220831bd Christos Stathis
                        }
313 9539e23d Christos Stathis
314 9539e23d Christos Stathis
                        @Override
315 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
316 9539e23d Christos Stathis
                                app.sessionExpired();
317 9539e23d Christos Stathis
                        }
318 220831bd Christos Stathis
                };
319 220831bd Christos Stathis
                getUserSharedContainers.setHeader("X-Auth-Token", app.getToken());
320 220831bd Christos Stathis
                Scheduler.get().scheduleDeferred(getUserSharedContainers);
321 220831bd Christos Stathis
        }
322 220831bd Christos Stathis
323 220831bd Christos Stathis
        protected void fetchSharedFiles(final String username, final ListDataProvider<Folder> dataProvider) {
324 220831bd Christos Stathis
                GetRequest<AccountResource> getUserSharedContainers = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), username, "?format=json") {
325 220831bd Christos Stathis
326 220831bd Christos Stathis
                        @Override
327 7811b9d1 Christos Stathis
                        public void onSuccess(AccountResource _result) {
328 220831bd Christos Stathis
                            final ListDataProvider<Folder> tempProvider = new ListDataProvider<Folder>();
329 7811b9d1 Christos Stathis
                                Iterator<Folder> iter = _result.getContainers().iterator();
330 dddb75e2 Christos Stathis
                                sharedFiles.get(username).clear();
331 220831bd Christos Stathis
                                fetchFolder(username, iter, tempProvider, new Command() {
332 220831bd Christos Stathis
                                        
333 220831bd Christos Stathis
                                        @Override
334 220831bd Christos Stathis
                                        public void execute() {
335 220831bd Christos Stathis
                                                dataProvider.getList().clear();
336 220831bd Christos Stathis
                                                dataProvider.getList().addAll(tempProvider.getList());
337 220831bd Christos Stathis
                                                app.showFiles(sharedFiles.get(username));
338 220831bd Christos Stathis
                                        }
339 220831bd Christos Stathis
                                });
340 220831bd Christos Stathis
                        }
341 220831bd Christos Stathis
342 220831bd Christos Stathis
                        @Override
343 220831bd Christos Stathis
                        public void onError(Throwable t) {
344 220831bd Christos Stathis
                GWT.log("Error getting account", t);
345 3f8622d4 Christos Stathis
                                app.setError(t);
346 220831bd Christos Stathis
                if (t instanceof RestException)
347 220831bd Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
348 220831bd Christos Stathis
                else
349 220831bd Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
350 220831bd Christos Stathis
                        }
351 9539e23d Christos Stathis
352 9539e23d Christos Stathis
                        @Override
353 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
354 9539e23d Christos Stathis
                                app.sessionExpired();
355 9539e23d Christos Stathis
                        }
356 220831bd Christos Stathis
                };
357 220831bd Christos Stathis
                getUserSharedContainers.setHeader("X-Auth-Token", app.getToken());
358 220831bd Christos Stathis
                Scheduler.get().scheduleDeferred(getUserSharedContainers);
359 220831bd Christos Stathis
        }
360 220831bd Christos Stathis
361 220831bd Christos Stathis
        protected void fetchFolder(final String username, final Iterator<Folder> iter, final ListDataProvider<Folder> dataProvider, final Command callback) {
362 6d8e0f58 Christos Stathis
        if (iter.hasNext()) {
363 6d8e0f58 Christos Stathis
            final Folder f = iter.next();
364 6d8e0f58 Christos Stathis
365 f5023f13 Christos Stathis
            String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
366 220831bd Christos Stathis
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), username, path, f) {
367 6d8e0f58 Christos Stathis
                @Override
368 7811b9d1 Christos Stathis
                public void onSuccess(Folder _result) {
369 7811b9d1 Christos Stathis
                        if (!_result.isShared()) {
370 7811b9d1 Christos Stathis
                                for (File file : _result.getFiles()) {
371 220831bd Christos Stathis
                                        if (file.isShared())
372 220831bd Christos Stathis
                                                sharedFiles.get(username).add(file);
373 220831bd Christos Stathis
                                }
374 7811b9d1 Christos Stathis
                                Iterator<Folder> iter2 = _result.getSubfolders().iterator();
375 220831bd Christos Stathis
                                fetchFolder(username, iter2, dataProvider, new Command() {
376 220831bd Christos Stathis
                                                        
377 220831bd Christos Stathis
                                                        @Override
378 220831bd Christos Stathis
                                                        public void execute() {
379 220831bd Christos Stathis
                                            fetchFolder(username, iter, dataProvider, callback);
380 220831bd Christos Stathis
                                                        }
381 220831bd Christos Stathis
                                                });
382 220831bd Christos Stathis
                        }
383 220831bd Christos Stathis
                        else {
384 7811b9d1 Christos Stathis
                                dataProvider.getList().add(_result);
385 220831bd Christos Stathis
                            fetchFolder(username, iter, dataProvider, callback);
386 220831bd Christos Stathis
                        }
387 6d8e0f58 Christos Stathis
                }
388 6d8e0f58 Christos Stathis
389 6d8e0f58 Christos Stathis
                @Override
390 6d8e0f58 Christos Stathis
                public void onError(Throwable t) {
391 6d8e0f58 Christos Stathis
                    GWT.log("Error getting folder", t);
392 3f8622d4 Christos Stathis
                                        app.setError(t);
393 6d8e0f58 Christos Stathis
                    if (t instanceof RestException)
394 6d8e0f58 Christos Stathis
                        app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
395 6d8e0f58 Christos Stathis
                    else
396 6d8e0f58 Christos Stathis
                        app.displayError("System error fetching folder: " + t.getMessage());
397 6d8e0f58 Christos Stathis
                }
398 9539e23d Christos Stathis
399 9539e23d Christos Stathis
                                @Override
400 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
401 9539e23d Christos Stathis
                                        app.sessionExpired();
402 9539e23d Christos Stathis
                                }
403 6d8e0f58 Christos Stathis
            };
404 6d8e0f58 Christos Stathis
            getFolder.setHeader("X-Auth-Token", app.getToken());
405 6d8e0f58 Christos Stathis
            Scheduler.get().scheduleDeferred(getFolder);
406 6d8e0f58 Christos Stathis
        }
407 6d8e0f58 Christos Stathis
        else if (callback != null)
408 6d8e0f58 Christos Stathis
            callback.execute();
409 6d8e0f58 Christos Stathis
    }
410 6d8e0f58 Christos Stathis
411 6d8e0f58 Christos Stathis
    public Folder getSelection() {
412 6d8e0f58 Christos Stathis
        return selectionModel.getSelectedObject();
413 6d8e0f58 Christos Stathis
    }
414 6d8e0f58 Christos Stathis
415 6d8e0f58 Christos Stathis
    public void updateFolder(Folder folder, boolean showfiles) {
416 6d8e0f58 Christos Stathis
        if (dataProviderMap.get(folder) == null) {
417 6d8e0f58 Christos Stathis
            dataProviderMap.put(folder, new ListDataProvider<Folder>());
418 6d8e0f58 Christos Stathis
        }
419 6d8e0f58 Christos Stathis
        final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
420 6d8e0f58 Christos Stathis
        fetchFolder(folder, dataProvider, showfiles);
421 6d8e0f58 Christos Stathis
    }
422 6d8e0f58 Christos Stathis
423 6d8e0f58 Christos Stathis
    public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider, final boolean showfiles) {
424 f5023f13 Christos Stathis
        String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
425 220831bd Christos Stathis
        GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
426 6d8e0f58 Christos Stathis
            @Override
427 7811b9d1 Christos Stathis
            public void onSuccess(final Folder _result) {
428 dddb75e2 Christos Stathis
                    GWT.log(String.valueOf(_result.getFiles().size()));
429 6d8e0f58 Christos Stathis
                if (showfiles)
430 7811b9d1 Christos Stathis
                    app.showFiles(_result);
431 7811b9d1 Christos Stathis
                Iterator<Folder> iter = _result.getSubfolders().iterator();
432 7811b9d1 Christos Stathis
                fetchFolder(_result.getOwner(), iter, dataProvider, new Command() {
433 6d8e0f58 Christos Stathis
                    @Override
434 6d8e0f58 Christos Stathis
                    public void execute() {
435 6d8e0f58 Christos Stathis
                        dataProvider.getList().clear();
436 7811b9d1 Christos Stathis
                        dataProvider.getList().addAll(_result.getSubfolders());
437 6d8e0f58 Christos Stathis
                        app.getOtherSharedTreeView().updateChildren(f);
438 6d8e0f58 Christos Stathis
                    }
439 6d8e0f58 Christos Stathis
                });
440 6d8e0f58 Christos Stathis
            }
441 6d8e0f58 Christos Stathis
442 6d8e0f58 Christos Stathis
            @Override
443 6d8e0f58 Christos Stathis
            public void onError(Throwable t) {
444 6d8e0f58 Christos Stathis
                GWT.log("Error getting folder", t);
445 3f8622d4 Christos Stathis
                                app.setError(t);
446 6d8e0f58 Christos Stathis
                if (t instanceof RestException)
447 6d8e0f58 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
448 6d8e0f58 Christos Stathis
                else
449 6d8e0f58 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
450 6d8e0f58 Christos Stathis
            }
451 9539e23d Christos Stathis
452 9539e23d Christos Stathis
                        @Override
453 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
454 9539e23d Christos Stathis
                                app.sessionExpired();
455 9539e23d Christos Stathis
                        }
456 6d8e0f58 Christos Stathis
        };
457 6d8e0f58 Christos Stathis
        getFolder.setHeader("X-Auth-Token", app.getToken());
458 6d8e0f58 Christos Stathis
        Scheduler.get().scheduleDeferred(getFolder);
459 6d8e0f58 Christos Stathis
    }
460 7c54d867 Christos Stathis
    
461 7c54d867 Christos Stathis
    public void initialize(Command callback) {
462 7c54d867 Christos Stathis
            fetchSharingUsers(callback);
463 7c54d867 Christos Stathis
    }
464 6d8e0f58 Christos Stathis
}