Finished new left pane
[pithos-web-client] / src / gr / grnet / pithos / web / client / mysharedtree / MysharedTreeViewModel.java
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.pithos.web.client.mysharedtree;
37
38 import gr.grnet.pithos.web.client.FolderContextMenu;
39 import gr.grnet.pithos.web.client.Pithos;
40 import gr.grnet.pithos.web.client.foldertree.AccountResource;
41 import gr.grnet.pithos.web.client.foldertree.File;
42 import gr.grnet.pithos.web.client.foldertree.Folder;
43 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
44 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView.Templates;
45 import gr.grnet.pithos.web.client.rest.GetRequest;
46 import gr.grnet.pithos.web.client.rest.RestException;
47
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.HashSet;
51 import java.util.Iterator;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Set;
55
56 import com.google.gwt.cell.client.AbstractCell;
57 import com.google.gwt.cell.client.Cell;
58 import com.google.gwt.cell.client.TextCell;
59 import com.google.gwt.cell.client.ValueUpdater;
60 import com.google.gwt.core.client.GWT;
61 import com.google.gwt.core.client.Scheduler;
62 import com.google.gwt.event.dom.client.ContextMenuEvent;
63 import com.google.gwt.http.client.Response;
64 import com.google.gwt.http.client.URL;
65 import com.google.gwt.safehtml.shared.SafeHtml;
66 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
67 import com.google.gwt.text.shared.SafeHtmlRenderer;
68 import com.google.gwt.user.client.Command;
69 import com.google.gwt.user.client.ui.AbstractImagePrototype;
70 import com.google.gwt.view.client.ListDataProvider;
71 import com.google.gwt.view.client.SelectionChangeEvent;
72 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
73 import com.google.gwt.view.client.SingleSelectionModel;
74 import com.google.gwt.view.client.TreeViewModel;
75
76 public class MysharedTreeViewModel implements TreeViewModel {
77
78     protected Pithos app;
79
80     private Cell<Folder> folderCell = new AbstractCell<Folder>() {
81
82        @Override
83         public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
84             String html = AbstractImagePrototype.create(MysharedTreeView.images.folderYellow()).getHTML();
85             safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
86             safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
87         }
88     };
89
90     protected ListDataProvider<Folder> firstLevelDataProvider = new ListDataProvider<Folder>();
91  
92     protected SingleSelectionModel<Folder> selectionModel;
93
94     public MysharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
95         app = _app;
96         this.selectionModel = selectionModel;
97     }
98
99     @Override
100     public <T> NodeInfo<?> getNodeInfo(T value) {
101         if (value == null) {
102                 fetchSharedContainers(null);
103             return new DefaultNodeInfo<Folder>(firstLevelDataProvider, folderCell, selectionModel, null);
104         }
105         return null;
106     }
107
108         private void fetchSharedContainers(final Command callback) {
109         String path = "?format=json&shared=";
110         GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), app.getUsername(), path) {
111             @Override
112             public void onSuccess(final AccountResource _result) {
113                                 firstLevelDataProvider.getList().clear();
114                 Folder t = null;
115                 for (Folder c : _result.getContainers()) {
116                         if (c.isHome())
117                                 firstLevelDataProvider.getList().add(0, c); //Pithos is always first
118                         else if (!c.isTrash())
119                                 firstLevelDataProvider.getList().add(c);
120                 }
121                                 if (callback != null)
122                                         callback.execute();
123             }
124
125             @Override
126             public void onError(Throwable t) {
127                 GWT.log("Error getting account", t);
128                                 app.setError(t);
129                 if (t instanceof RestException)
130                     app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
131                 else
132                     app.displayError("System error fetching user data: " + t.getMessage());
133             }
134
135                         @Override
136                         protected void onUnauthorized(Response response) {
137                                 app.sessionExpired();
138                         }
139         };
140         getAccount.setHeader("X-Auth-Token", app.getToken());
141         Scheduler.get().scheduleDeferred(getAccount);
142         }
143
144         @Override
145     public boolean isLeaf(Object o) {
146                 if (o == null)
147                         return false;
148                 return true;
149     }
150         
151         private native void log(String msg) /*-{
152                 $wnd.console.log(msg);
153         }-*/;
154
155     protected void fetchFolder(final Iterator<Folder> iter, final Command callback) {
156         if (iter.hasNext()) {
157             final Folder f = iter.next();
158
159             String path = "/" + f.getContainer() + "?format=json&shared=&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
160             GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
161                 @Override
162                 public void onSuccess(Folder _result) {
163                     fetchFolder(iter, callback);
164                 }
165
166                 @Override
167                 public void onError(Throwable t) {
168                     GWT.log("Error getting folder", t);
169                                         app.setError(t);
170                     if (t instanceof RestException)
171                         app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
172                     else
173                         app.displayError("System error fetching folder: " + t.getMessage());
174                 }
175
176                                 @Override
177                                 protected void onUnauthorized(Response response) {
178                                         app.sessionExpired();
179                                 }
180             };
181             getFolder.setHeader("X-Auth-Token", app.getToken());
182             Scheduler.get().scheduleDeferred(getFolder);
183         }
184         else if (callback != null)
185             callback.execute();
186     }
187
188     public Folder getSelection() {
189         return selectionModel.getSelectedObject();
190     }
191
192     public void updateFolder(Folder folder, boolean showfiles) {
193         fetchFolder(folder,showfiles);
194     }
195
196     public void fetchFolder(final Folder f, final boolean showfiles) {
197         String path = "/" + f.getContainer() + "?format=json&shared=" + URL.encodeQueryString(f.getPrefix());
198         GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, null) {
199             @Override
200             public void onSuccess(final Folder _result) {
201                 if (showfiles)
202                     app.showFiles(_result);
203             }
204
205             @Override
206             public void onError(Throwable t) {
207                 GWT.log("Error getting folder", t);
208                                 app.setError(t);
209                 if (t instanceof RestException)
210                     app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
211                 else
212                     app.displayError("System error fetching folder: " + t.getMessage());
213             }
214
215                         @Override
216                         protected void onUnauthorized(Response response) {
217                                 app.sessionExpired();
218                         }
219         };
220         getFolder.setHeader("X-Auth-Token", app.getToken());
221         Scheduler.get().scheduleDeferred(getFolder);
222     }
223
224         public void initialize(Command callback) {
225                 fetchSharedContainers(callback);
226         }
227 }