Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (18.7 kB)

1 6d8e0f58 Christos Stathis
/*
2 6d8e0f58 Christos Stathis
 * Copyright 2011 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 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView.Templates;
45 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
46 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
47 6d8e0f58 Christos Stathis
48 6d8e0f58 Christos Stathis
import java.util.HashMap;
49 220831bd Christos Stathis
import java.util.HashSet;
50 6d8e0f58 Christos Stathis
import java.util.Iterator;
51 6d8e0f58 Christos Stathis
import java.util.Map;
52 220831bd Christos Stathis
import java.util.Set;
53 6d8e0f58 Christos Stathis
54 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
55 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.Cell;
56 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.TextCell;
57 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.ValueUpdater;
58 6d8e0f58 Christos Stathis
import com.google.gwt.core.client.GWT;
59 6d8e0f58 Christos Stathis
import com.google.gwt.core.client.Scheduler;
60 6d8e0f58 Christos Stathis
import com.google.gwt.event.dom.client.ContextMenuEvent;
61 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
62 f5023f13 Christos Stathis
import com.google.gwt.http.client.URL;
63 6d8e0f58 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtml;
64 6d8e0f58 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
65 6d8e0f58 Christos Stathis
import com.google.gwt.text.shared.SafeHtmlRenderer;
66 6d8e0f58 Christos Stathis
import com.google.gwt.user.client.Command;
67 6d8e0f58 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
68 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
69 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
70 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
71 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
72 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
73 6d8e0f58 Christos Stathis
74 6d8e0f58 Christos Stathis
public class OtherSharedTreeViewModel implements TreeViewModel {
75 6d8e0f58 Christos Stathis
76 7c54d867 Christos Stathis
        private static final String treeTitle = "Other 's shared";
77 6d8e0f58 Christos Stathis
    protected Pithos app;
78 6d8e0f58 Christos Stathis
79 6d8e0f58 Christos Stathis
    private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) {
80 6d8e0f58 Christos Stathis
81 6d8e0f58 Christos Stathis
       @Override
82 7811b9d1 Christos Stathis
        public void render(@SuppressWarnings("unused") Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
83 6d8e0f58 Christos Stathis
            String html = AbstractImagePrototype.create(OtherSharedTreeView.images.folderYellow()).getHTML();
84 a7b6e464 Christos Stathis
            safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
85 6d8e0f58 Christos Stathis
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
86 6d8e0f58 Christos Stathis
        }
87 6d8e0f58 Christos Stathis
88 6d8e0f58 Christos Stathis
        @Override
89 7811b9d1 Christos Stathis
        public void onBrowserEvent(@SuppressWarnings("unused") Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, Folder folder, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") ValueUpdater<Folder> valueUpdater) {
90 6d8e0f58 Christos Stathis
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
91 6d8e0f58 Christos Stathis
                OtherSharedTreeViewModel.this.selectionModel.setSelected(folder, true);
92 02d3a335 Christos Stathis
                FolderContextMenu menu = new FolderContextMenu(app, OtherSharedTreeView.images, app.getSelectedTree(), folder);
93 6d8e0f58 Christos Stathis
                menu.setPopupPosition(event.getClientX(), event.getClientY());
94 6d8e0f58 Christos Stathis
                menu.show();
95 6d8e0f58 Christos Stathis
            }
96 6d8e0f58 Christos Stathis
        }
97 6d8e0f58 Christos Stathis
    };
98 6d8e0f58 Christos Stathis
99 6d8e0f58 Christos Stathis
    private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
100 220831bd Christos Stathis
    protected ListDataProvider<String> userLevelDataProvider = new ListDataProvider<String>();
101 6d8e0f58 Christos Stathis
102 220831bd Christos Stathis
    protected Map<String, ListDataProvider<Folder>> userDataProviderMap = new HashMap<String, ListDataProvider<Folder>>();
103 6d8e0f58 Christos Stathis
    private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>();
104 220831bd Christos Stathis
    
105 220831bd Christos Stathis
    protected Map<String, Set<File>> sharedFiles = new HashMap<String, Set<File>>();
106 6d8e0f58 Christos Stathis
107 220831bd Christos Stathis
    protected SingleSelectionModel<Folder> selectionModel;
108 6d8e0f58 Christos Stathis
109 6d8e0f58 Christos Stathis
    public OtherSharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
110 6d8e0f58 Christos Stathis
        app = _app;
111 6d8e0f58 Christos Stathis
        this.selectionModel = selectionModel;
112 6d8e0f58 Christos Stathis
    }
113 6d8e0f58 Christos Stathis
114 6d8e0f58 Christos Stathis
    @Override
115 6d8e0f58 Christos Stathis
    public <T> NodeInfo<?> getNodeInfo(T value) {
116 6d8e0f58 Christos Stathis
        if (value == null) {
117 7c54d867 Christos Stathis
            rootDataProvider.getList().add(treeTitle);
118 6d8e0f58 Christos Stathis
            final SingleSelectionModel<String> selectionModel2 = new SingleSelectionModel<String>();
119 6d8e0f58 Christos Stathis
            selectionModel2.addSelectionChangeHandler(new Handler() {
120 6d8e0f58 Christos Stathis
121 6d8e0f58 Christos Stathis
                @Override
122 7811b9d1 Christos Stathis
                public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
123 6d8e0f58 Christos Stathis
                    if (selectionModel2.getSelectedObject() != null) {
124 f1b24b82 Christos Stathis
                            app.deselectOthers(app.getOtherSharedTreeView(), selectionModel2);
125 3ebb88f4 Christos Stathis
                            app.applyPermissions(null);
126 220831bd Christos Stathis
                            app.showFiles(new HashSet<File>());
127 6d8e0f58 Christos Stathis
                    }
128 6d8e0f58 Christos Stathis
                }
129 6d8e0f58 Christos Stathis
            });
130 6d8e0f58 Christos Stathis
            app.addSelectionModel(selectionModel2);
131 6d8e0f58 Christos Stathis
            return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
132 6d8e0f58 Christos Stathis
                @Override
133 6d8e0f58 Christos Stathis
                public SafeHtml render(String object) {
134 6d8e0f58 Christos Stathis
                    SafeHtmlBuilder builder = new SafeHtmlBuilder();
135 6d8e0f58 Christos Stathis
                    render(object, builder);
136 6d8e0f58 Christos Stathis
                    return builder.toSafeHtml();
137 6d8e0f58 Christos Stathis
                }
138 6d8e0f58 Christos Stathis
139 6d8e0f58 Christos Stathis
                @Override
140 6d8e0f58 Christos Stathis
                public void render(String object, SafeHtmlBuilder builder) {
141 6d8e0f58 Christos Stathis
                    String html = AbstractImagePrototype.create(OtherSharedTreeView.images.othersShared()).getHTML();
142 a7b6e464 Christos Stathis
                    builder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
143 6d8e0f58 Christos Stathis
                    builder.append(OtherSharedTreeView.Templates.INSTANCE.nameSpan(object));
144 6d8e0f58 Christos Stathis
                }
145 6d8e0f58 Christos Stathis
            }),  selectionModel2, null);
146 6d8e0f58 Christos Stathis
        }
147 6d8e0f58 Christos Stathis
        else if (value instanceof String) {
148 7c54d867 Christos Stathis
                if (value.equals(treeTitle)) {
149 7c54d867 Christos Stathis
                        fetchSharingUsers(null);
150 6d8e0f58 Christos Stathis
                    final SingleSelectionModel<String> selectionModel3 = new SingleSelectionModel<String>();
151 6d8e0f58 Christos Stathis
                    selectionModel3.addSelectionChangeHandler(new Handler() {
152 6d8e0f58 Christos Stathis
153 6d8e0f58 Christos Stathis
                        @Override
154 7811b9d1 Christos Stathis
                        public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
155 6d8e0f58 Christos Stathis
                            if (selectionModel3.getSelectedObject() != null) {
156 f1b24b82 Christos Stathis
                                    app.deselectOthers(app.getOtherSharedTreeView(), selectionModel3);
157 3ebb88f4 Christos Stathis
                                    app.applyPermissions(null);
158 220831bd Christos Stathis
                                    String username = selectionModel3.getSelectedObject();
159 63ee965c Christos Stathis
                                            if (userDataProviderMap.get(username) == null) {
160 63ee965c Christos Stathis
                                                    userDataProviderMap.put(username, new ListDataProvider<Folder>());
161 63ee965c Christos Stathis
                                            }
162 220831bd Christos Stathis
                                    fetchSharedFiles(username, userDataProviderMap.get(username));
163 6d8e0f58 Christos Stathis
                            }
164 6d8e0f58 Christos Stathis
                        }
165 6d8e0f58 Christos Stathis
                    });
166 6d8e0f58 Christos Stathis
                    app.addSelectionModel(selectionModel3);
167 220831bd Christos Stathis
                    return new DefaultNodeInfo<String>(userLevelDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
168 6d8e0f58 Christos Stathis
169 6d8e0f58 Christos Stathis
                                        @Override
170 6d8e0f58 Christos Stathis
                                        public SafeHtml render(String object) {
171 6d8e0f58 Christos Stathis
                            SafeHtmlBuilder builder = new SafeHtmlBuilder();
172 6d8e0f58 Christos Stathis
                            render(object, builder);
173 6d8e0f58 Christos Stathis
                            return builder.toSafeHtml();
174 6d8e0f58 Christos Stathis
                                        }
175 6d8e0f58 Christos Stathis
176 6d8e0f58 Christos Stathis
                                        @Override
177 6d8e0f58 Christos Stathis
                                        public void render(String object, SafeHtmlBuilder builder) {
178 220831bd Christos Stathis
                            String html = AbstractImagePrototype.create(OtherSharedTreeView.images.user()).getHTML();
179 a7b6e464 Christos Stathis
                            builder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
180 6d8e0f58 Christos Stathis
                            builder.append(OtherSharedTreeView.Templates.INSTANCE.nameSpan(object));
181 6d8e0f58 Christos Stathis
                                        }
182 6d8e0f58 Christos Stathis
                                }), selectionModel3, null);
183 6d8e0f58 Christos Stathis
                }
184 220831bd Christos Stathis
                        String username = (String) value;
185 220831bd Christos Stathis
                        if (userDataProviderMap.get(username) == null) {
186 220831bd Christos Stathis
                                userDataProviderMap.put(username, new ListDataProvider<Folder>());
187 220831bd Christos Stathis
                        }
188 220831bd Christos Stathis
                        final ListDataProvider<Folder> dataProvider = userDataProviderMap.get(username);
189 7c54d867 Christos Stathis
                        fetchSharedContainers(username, dataProvider, null);
190 220831bd Christos Stathis
                        return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
191 6d8e0f58 Christos Stathis
        }
192 6d8e0f58 Christos Stathis
        else {
193 6d8e0f58 Christos Stathis
            final Folder f = (Folder) value;
194 6d8e0f58 Christos Stathis
            if (dataProviderMap.get(f) == null) {
195 6d8e0f58 Christos Stathis
                dataProviderMap.put(f, new ListDataProvider<Folder>());
196 6d8e0f58 Christos Stathis
            }
197 6d8e0f58 Christos Stathis
            final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
198 6d8e0f58 Christos Stathis
            fetchFolder(f, dataProvider, false);
199 6d8e0f58 Christos Stathis
            return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
200 6d8e0f58 Christos Stathis
        }
201 6d8e0f58 Christos Stathis
    }
202 6d8e0f58 Christos Stathis
203 7c54d867 Christos Stathis
    private void fetchSharingUsers(final Command callback) {
204 6d8e0f58 Christos Stathis
        GetRequest<SharingUsers> getSharingUsers = new GetRequest<SharingUsers>(SharingUsers.class, app.getApiPath(), "", "?format=json") {
205 6d8e0f58 Christos Stathis
            @Override
206 7811b9d1 Christos Stathis
            public void onSuccess(final SharingUsers _result) {
207 220831bd Christos Stathis
                userLevelDataProvider.getList().clear();
208 7811b9d1 Christos Stathis
                userLevelDataProvider.getList().addAll(_result.getUsers());
209 7811b9d1 Christos Stathis
                for (String name : _result.getUsers()) {
210 220831bd Christos Stathis
                        sharedFiles.put(name, new HashSet<File>());
211 220831bd Christos Stathis
                }
212 7c54d867 Christos Stathis
                Iterator<String> iter = _result.getUsers().iterator();
213 7c54d867 Christos Stathis
                fetchSharedContainers(iter, callback);
214 6d8e0f58 Christos Stathis
            }
215 6d8e0f58 Christos Stathis
216 6d8e0f58 Christos Stathis
            @Override
217 6d8e0f58 Christos Stathis
            public void onError(Throwable t) {
218 6d8e0f58 Christos Stathis
                GWT.log("Error getting folder", t);
219 3f8622d4 Christos Stathis
                                app.setError(t);
220 6d8e0f58 Christos Stathis
                if (t instanceof RestException)
221 6d8e0f58 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
222 6d8e0f58 Christos Stathis
                else
223 6d8e0f58 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
224 6d8e0f58 Christos Stathis
            }
225 9539e23d Christos Stathis
226 9539e23d Christos Stathis
                        @Override
227 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
228 9539e23d Christos Stathis
                                app.sessionExpired();
229 9539e23d Christos Stathis
                        }
230 6d8e0f58 Christos Stathis
        };
231 6d8e0f58 Christos Stathis
        getSharingUsers.setHeader("X-Auth-Token", app.getToken());
232 6d8e0f58 Christos Stathis
        Scheduler.get().scheduleDeferred(getSharingUsers);
233 6d8e0f58 Christos Stathis
        }
234 6d8e0f58 Christos Stathis
235 7c54d867 Christos Stathis
        protected void fetchSharedContainers(final Iterator<String> iter, final Command callback) {
236 7c54d867 Christos Stathis
                if (iter.hasNext()) {
237 7c54d867 Christos Stathis
                        String username = iter.next();
238 7c54d867 Christos Stathis
                        if (userDataProviderMap.get(username) == null) {
239 7c54d867 Christos Stathis
                                userDataProviderMap.put(username, new ListDataProvider<Folder>());
240 7c54d867 Christos Stathis
                        }
241 7c54d867 Christos Stathis
                        final ListDataProvider<Folder> dataProvider = userDataProviderMap.get(username);
242 7c54d867 Christos Stathis
                        fetchSharedContainers(username, dataProvider, new Command() {
243 7c54d867 Christos Stathis
                                
244 7c54d867 Christos Stathis
                                @Override
245 7c54d867 Christos Stathis
                                public void execute() {
246 7c54d867 Christos Stathis
                                        fetchSharedContainers(iter, callback);
247 7c54d867 Christos Stathis
                                        
248 7c54d867 Christos Stathis
                                }
249 7c54d867 Christos Stathis
                        });
250 7c54d867 Christos Stathis
                }
251 7c54d867 Christos Stathis
                else
252 7c54d867 Christos Stathis
                        if (callback != null)
253 7c54d867 Christos Stathis
                                callback.execute();
254 7c54d867 Christos Stathis
        }
255 7c54d867 Christos Stathis
256 6d8e0f58 Christos Stathis
        @Override
257 6d8e0f58 Christos Stathis
    public boolean isLeaf(Object o) {
258 7c54d867 Christos Stathis
                if (o == null)
259 7c54d867 Christos Stathis
                        return false;
260 7c54d867 Christos Stathis
                else if (o instanceof Folder) {
261 6d8e0f58 Christos Stathis
            Folder f = (Folder) o;
262 6d8e0f58 Christos Stathis
            return f.getSubfolders().isEmpty();
263 6d8e0f58 Christos Stathis
        }
264 7c54d867 Christos Stathis
                else {
265 7c54d867 Christos Stathis
                        if (o.equals(treeTitle))
266 7c54d867 Christos Stathis
                                return userLevelDataProvider.getList().isEmpty();
267 7c54d867 Christos Stathis
                        ListDataProvider<Folder> dp = userDataProviderMap.get(o);
268 7c54d867 Christos Stathis
                        if (dp != null)
269 7c54d867 Christos Stathis
                                return dp.getList().isEmpty();
270 7c54d867 Christos Stathis
                        return true;
271 7c54d867 Christos Stathis
                }
272 6d8e0f58 Christos Stathis
    }
273 6d8e0f58 Christos Stathis
274 7c54d867 Christos Stathis
        private void fetchSharedContainers(final String username, final ListDataProvider<Folder> dataProvider, final Command callback) {
275 220831bd Christos Stathis
                GetRequest<AccountResource> getUserSharedContainers = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), username, "?format=json") {
276 220831bd Christos Stathis
277 220831bd Christos Stathis
                        @Override
278 7811b9d1 Christos Stathis
                        public void onSuccess(AccountResource _result) {
279 220831bd Christos Stathis
                            final ListDataProvider<Folder> tempProvider = new ListDataProvider<Folder>();
280 7811b9d1 Christos Stathis
                                Iterator<Folder> iter = _result.getContainers().iterator();
281 220831bd Christos Stathis
                                fetchFolder(username, iter, tempProvider, new Command() {
282 220831bd Christos Stathis
                                        
283 220831bd Christos Stathis
                                        @Override
284 220831bd Christos Stathis
                                        public void execute() {
285 220831bd Christos Stathis
                                                dataProvider.getList().clear();
286 220831bd Christos Stathis
                                                dataProvider.getList().addAll(tempProvider.getList());
287 7c54d867 Christos Stathis
                                                if (callback != null)
288 7c54d867 Christos Stathis
                                                        callback.execute();
289 220831bd Christos Stathis
                                        }
290 220831bd Christos Stathis
                                });
291 220831bd Christos Stathis
                        }
292 220831bd Christos Stathis
293 220831bd Christos Stathis
                        @Override
294 220831bd Christos Stathis
                        public void onError(Throwable t) {
295 220831bd Christos Stathis
                GWT.log("Error getting account", t);
296 3f8622d4 Christos Stathis
                                app.setError(t);
297 220831bd Christos Stathis
                if (t instanceof RestException)
298 220831bd Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
299 220831bd Christos Stathis
                else
300 220831bd Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
301 220831bd Christos Stathis
                        }
302 9539e23d Christos Stathis
303 9539e23d Christos Stathis
                        @Override
304 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
305 9539e23d Christos Stathis
                                app.sessionExpired();
306 9539e23d Christos Stathis
                        }
307 220831bd Christos Stathis
                };
308 220831bd Christos Stathis
                getUserSharedContainers.setHeader("X-Auth-Token", app.getToken());
309 220831bd Christos Stathis
                Scheduler.get().scheduleDeferred(getUserSharedContainers);
310 220831bd Christos Stathis
        }
311 220831bd Christos Stathis
312 220831bd Christos Stathis
        protected void fetchSharedFiles(final String username, final ListDataProvider<Folder> dataProvider) {
313 220831bd Christos Stathis
                GetRequest<AccountResource> getUserSharedContainers = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), username, "?format=json") {
314 220831bd Christos Stathis
315 220831bd Christos Stathis
                        @Override
316 7811b9d1 Christos Stathis
                        public void onSuccess(AccountResource _result) {
317 220831bd Christos Stathis
                            final ListDataProvider<Folder> tempProvider = new ListDataProvider<Folder>();
318 7811b9d1 Christos Stathis
                                Iterator<Folder> iter = _result.getContainers().iterator();
319 220831bd Christos Stathis
                                fetchFolder(username, iter, tempProvider, new Command() {
320 220831bd Christos Stathis
                                        
321 220831bd Christos Stathis
                                        @Override
322 220831bd Christos Stathis
                                        public void execute() {
323 220831bd Christos Stathis
                                                dataProvider.getList().clear();
324 220831bd Christos Stathis
                                                dataProvider.getList().addAll(tempProvider.getList());
325 220831bd Christos Stathis
                                                app.showFiles(sharedFiles.get(username));
326 220831bd Christos Stathis
                                        }
327 220831bd Christos Stathis
                                });
328 220831bd Christos Stathis
                        }
329 220831bd Christos Stathis
330 220831bd Christos Stathis
                        @Override
331 220831bd Christos Stathis
                        public void onError(Throwable t) {
332 220831bd Christos Stathis
                GWT.log("Error getting account", t);
333 3f8622d4 Christos Stathis
                                app.setError(t);
334 220831bd Christos Stathis
                if (t instanceof RestException)
335 220831bd Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
336 220831bd Christos Stathis
                else
337 220831bd Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
338 220831bd Christos Stathis
                        }
339 9539e23d Christos Stathis
340 9539e23d Christos Stathis
                        @Override
341 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
342 9539e23d Christos Stathis
                                app.sessionExpired();
343 9539e23d Christos Stathis
                        }
344 220831bd Christos Stathis
                };
345 220831bd Christos Stathis
                getUserSharedContainers.setHeader("X-Auth-Token", app.getToken());
346 220831bd Christos Stathis
                Scheduler.get().scheduleDeferred(getUserSharedContainers);
347 220831bd Christos Stathis
        }
348 220831bd Christos Stathis
349 220831bd Christos Stathis
        protected void fetchFolder(final String username, final Iterator<Folder> iter, final ListDataProvider<Folder> dataProvider, final Command callback) {
350 6d8e0f58 Christos Stathis
        if (iter.hasNext()) {
351 6d8e0f58 Christos Stathis
            final Folder f = iter.next();
352 6d8e0f58 Christos Stathis
353 f5023f13 Christos Stathis
            String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
354 220831bd Christos Stathis
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), username, path, f) {
355 6d8e0f58 Christos Stathis
                @Override
356 7811b9d1 Christos Stathis
                public void onSuccess(Folder _result) {
357 7811b9d1 Christos Stathis
                        if (!_result.isShared()) {
358 7811b9d1 Christos Stathis
                                for (File file : _result.getFiles()) {
359 220831bd Christos Stathis
                                        if (file.isShared())
360 220831bd Christos Stathis
                                                sharedFiles.get(username).add(file);
361 220831bd Christos Stathis
                                }
362 7811b9d1 Christos Stathis
                                Iterator<Folder> iter2 = _result.getSubfolders().iterator();
363 220831bd Christos Stathis
                                fetchFolder(username, iter2, dataProvider, new Command() {
364 220831bd Christos Stathis
                                                        
365 220831bd Christos Stathis
                                                        @Override
366 220831bd Christos Stathis
                                                        public void execute() {
367 220831bd Christos Stathis
                                            fetchFolder(username, iter, dataProvider, callback);
368 220831bd Christos Stathis
                                                        }
369 220831bd Christos Stathis
                                                });
370 220831bd Christos Stathis
                        }
371 220831bd Christos Stathis
                        else {
372 7811b9d1 Christos Stathis
                                dataProvider.getList().add(_result);
373 220831bd Christos Stathis
                            fetchFolder(username, iter, dataProvider, callback);
374 220831bd Christos Stathis
                        }
375 6d8e0f58 Christos Stathis
                }
376 6d8e0f58 Christos Stathis
377 6d8e0f58 Christos Stathis
                @Override
378 6d8e0f58 Christos Stathis
                public void onError(Throwable t) {
379 6d8e0f58 Christos Stathis
                    GWT.log("Error getting folder", t);
380 3f8622d4 Christos Stathis
                                        app.setError(t);
381 6d8e0f58 Christos Stathis
                    if (t instanceof RestException)
382 6d8e0f58 Christos Stathis
                        app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
383 6d8e0f58 Christos Stathis
                    else
384 6d8e0f58 Christos Stathis
                        app.displayError("System error fetching folder: " + t.getMessage());
385 6d8e0f58 Christos Stathis
                }
386 9539e23d Christos Stathis
387 9539e23d Christos Stathis
                                @Override
388 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
389 9539e23d Christos Stathis
                                        app.sessionExpired();
390 9539e23d Christos Stathis
                                }
391 6d8e0f58 Christos Stathis
            };
392 6d8e0f58 Christos Stathis
            getFolder.setHeader("X-Auth-Token", app.getToken());
393 6d8e0f58 Christos Stathis
            Scheduler.get().scheduleDeferred(getFolder);
394 6d8e0f58 Christos Stathis
        }
395 6d8e0f58 Christos Stathis
        else if (callback != null)
396 6d8e0f58 Christos Stathis
            callback.execute();
397 6d8e0f58 Christos Stathis
    }
398 6d8e0f58 Christos Stathis
399 6d8e0f58 Christos Stathis
    public Folder getSelection() {
400 6d8e0f58 Christos Stathis
        return selectionModel.getSelectedObject();
401 6d8e0f58 Christos Stathis
    }
402 6d8e0f58 Christos Stathis
403 6d8e0f58 Christos Stathis
    public void updateFolder(Folder folder, boolean showfiles) {
404 6d8e0f58 Christos Stathis
        if (dataProviderMap.get(folder) == null) {
405 6d8e0f58 Christos Stathis
            dataProviderMap.put(folder, new ListDataProvider<Folder>());
406 6d8e0f58 Christos Stathis
        }
407 6d8e0f58 Christos Stathis
        final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
408 6d8e0f58 Christos Stathis
        fetchFolder(folder, dataProvider, showfiles);
409 6d8e0f58 Christos Stathis
    }
410 6d8e0f58 Christos Stathis
411 6d8e0f58 Christos Stathis
    public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider, final boolean showfiles) {
412 f5023f13 Christos Stathis
        String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
413 220831bd Christos Stathis
        GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
414 6d8e0f58 Christos Stathis
            @Override
415 7811b9d1 Christos Stathis
            public void onSuccess(final Folder _result) {
416 6d8e0f58 Christos Stathis
                if (showfiles)
417 7811b9d1 Christos Stathis
                    app.showFiles(_result);
418 7811b9d1 Christos Stathis
                Iterator<Folder> iter = _result.getSubfolders().iterator();
419 7811b9d1 Christos Stathis
                fetchFolder(_result.getOwner(), iter, dataProvider, new Command() {
420 6d8e0f58 Christos Stathis
                    @Override
421 6d8e0f58 Christos Stathis
                    public void execute() {
422 6d8e0f58 Christos Stathis
                        dataProvider.getList().clear();
423 7811b9d1 Christos Stathis
                        dataProvider.getList().addAll(_result.getSubfolders());
424 6d8e0f58 Christos Stathis
                        app.getOtherSharedTreeView().updateChildren(f);
425 6d8e0f58 Christos Stathis
                    }
426 6d8e0f58 Christos Stathis
                });
427 6d8e0f58 Christos Stathis
            }
428 6d8e0f58 Christos Stathis
429 6d8e0f58 Christos Stathis
            @Override
430 6d8e0f58 Christos Stathis
            public void onError(Throwable t) {
431 6d8e0f58 Christos Stathis
                GWT.log("Error getting folder", t);
432 3f8622d4 Christos Stathis
                                app.setError(t);
433 6d8e0f58 Christos Stathis
                if (t instanceof RestException)
434 6d8e0f58 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
435 6d8e0f58 Christos Stathis
                else
436 6d8e0f58 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
437 6d8e0f58 Christos Stathis
            }
438 9539e23d Christos Stathis
439 9539e23d Christos Stathis
                        @Override
440 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
441 9539e23d Christos Stathis
                                app.sessionExpired();
442 9539e23d Christos Stathis
                        }
443 6d8e0f58 Christos Stathis
        };
444 6d8e0f58 Christos Stathis
        getFolder.setHeader("X-Auth-Token", app.getToken());
445 6d8e0f58 Christos Stathis
        Scheduler.get().scheduleDeferred(getFolder);
446 6d8e0f58 Christos Stathis
    }
447 7c54d867 Christos Stathis
    
448 7c54d867 Christos Stathis
    public void initialize(Command callback) {
449 7c54d867 Christos Stathis
            fetchSharingUsers(callback);
450 7c54d867 Christos Stathis
    }
451 6d8e0f58 Christos Stathis
}