root / src / gr / grnet / pithos / web / client / FilePermissionsDialog.java @ 5ec7b091
History | View | Annotate | Download (12.9 kB)
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 |
package gr.grnet.pithos.web.client; |
36 |
|
37 |
import gr.grnet.pithos.web.client.foldertree.File; |
38 |
import gr.grnet.pithos.web.client.foldertree.Resource; |
39 |
import gr.grnet.pithos.web.client.rest.HeadRequest; |
40 |
import gr.grnet.pithos.web.client.rest.PostRequest; |
41 |
|
42 |
import java.util.Map; |
43 |
|
44 |
import com.google.gwt.core.client.GWT; |
45 |
import com.google.gwt.core.client.Scheduler; |
46 |
import com.google.gwt.dom.client.NativeEvent; |
47 |
import com.google.gwt.event.dom.client.ClickEvent; |
48 |
import com.google.gwt.event.dom.client.ClickHandler; |
49 |
import com.google.gwt.event.dom.client.KeyCodes; |
50 |
import com.google.gwt.http.client.Response; |
51 |
import com.google.gwt.http.client.URL; |
52 |
import com.google.gwt.http.client.UrlBuilder; |
53 |
import com.google.gwt.i18n.client.Dictionary; |
54 |
import com.google.gwt.resources.client.ImageResource; |
55 |
import com.google.gwt.user.client.Command; |
56 |
import com.google.gwt.user.client.Window; |
57 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
58 |
import com.google.gwt.user.client.ui.Anchor; |
59 |
import com.google.gwt.user.client.ui.Button; |
60 |
import com.google.gwt.user.client.ui.CheckBox; |
61 |
import com.google.gwt.user.client.ui.FocusPanel; |
62 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
63 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
64 |
import com.google.gwt.user.client.ui.Label; |
65 |
import com.google.gwt.user.client.ui.TextBox; |
66 |
import com.google.gwt.user.client.ui.VerticalPanel; |
67 |
|
68 |
/**
|
69 |
* The 'File properties' dialog box implementation.
|
70 |
*
|
71 |
*/
|
72 |
public class FilePermissionsDialog extends AbstractPropertiesDialog { |
73 |
|
74 |
protected PermissionsList permList;
|
75 |
|
76 |
private HorizontalPanel pathPanel;
|
77 |
|
78 |
private TextBox path;
|
79 |
|
80 |
/**
|
81 |
* An image bundle for this widgets images.
|
82 |
*/
|
83 |
public interface Images extends MessagePanel.Images { |
84 |
|
85 |
@Source("gr/grnet/pithos/resources/edit_user.png") |
86 |
ImageResource permUser(); |
87 |
|
88 |
@Source("gr/grnet/pithos/resources/groups22.png") |
89 |
ImageResource permGroup(); |
90 |
|
91 |
@Source("gr/grnet/pithos/resources/delete.gif") |
92 |
ImageResource delete(); |
93 |
} |
94 |
|
95 |
final File file; |
96 |
|
97 |
Images images = GWT.create(Images.class); |
98 |
|
99 |
/**
|
100 |
* The widget's constructor.
|
101 |
*/
|
102 |
public FilePermissionsDialog(Pithos _app, File _file) { |
103 |
super(_app);
|
104 |
file = _file; |
105 |
|
106 |
Anchor close = new Anchor();
|
107 |
close.addStyleName("close");
|
108 |
close.addClickHandler(new ClickHandler() {
|
109 |
|
110 |
@Override
|
111 |
public void onClick(ClickEvent event) { |
112 |
hide(); |
113 |
} |
114 |
}); |
115 |
// Set the dialog's caption.
|
116 |
setText("File permissions");
|
117 |
setAnimationEnabled(true);
|
118 |
setGlassEnabled(true);
|
119 |
setStyleName("pithos-DialogBox");
|
120 |
|
121 |
// Outer contains inner and buttons.
|
122 |
final VerticalPanel outer = new VerticalPanel(); |
123 |
outer.add(close); |
124 |
final FocusPanel focusPanel = new FocusPanel(outer); |
125 |
// Inner contains generalPanel and permPanel.
|
126 |
inner = new VerticalPanel();
|
127 |
inner.addStyleName("inner");
|
128 |
|
129 |
inner.add(createSharingPanel()); |
130 |
|
131 |
outer.add(inner); |
132 |
|
133 |
final Button ok = new Button("Close", new ClickHandler() { |
134 |
@Override
|
135 |
public void onClick(ClickEvent event) { |
136 |
closeDialog(); |
137 |
} |
138 |
}); |
139 |
ok.addStyleName("button");
|
140 |
|
141 |
outer.add(ok); |
142 |
outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER); |
143 |
|
144 |
focusPanel.setFocus(true);
|
145 |
setWidget(outer); |
146 |
} |
147 |
|
148 |
private VerticalPanel createSharingPanel() {
|
149 |
VerticalPanel permPanel = new VerticalPanel();
|
150 |
|
151 |
permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), false, new Command() { |
152 |
|
153 |
@Override
|
154 |
public void execute() { |
155 |
accept(); |
156 |
} |
157 |
}); |
158 |
permPanel.add(permList); |
159 |
|
160 |
HorizontalPanel permButtons = new HorizontalPanel();
|
161 |
final Button addUser = new Button("Add User", new ClickHandler() { |
162 |
@Override
|
163 |
public void onClick(ClickEvent event) { |
164 |
PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true); |
165 |
dlg.center(); |
166 |
permList.updatePermissionTable(); |
167 |
} |
168 |
}); |
169 |
addUser.addStyleName("button");
|
170 |
permButtons.add(addUser); |
171 |
permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER); |
172 |
|
173 |
Button add = new Button("Add Group", new ClickHandler() { |
174 |
@Override
|
175 |
public void onClick(ClickEvent event) { |
176 |
if (app.getAccount().getGroups().isEmpty()) {
|
177 |
new GroupCreateDialog(app, new Command() { |
178 |
|
179 |
@Override
|
180 |
public void execute() { |
181 |
if (app.getAccount().getGroups().isEmpty())
|
182 |
return;
|
183 |
PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false); |
184 |
dlg.center(); |
185 |
permList.updatePermissionTable(); |
186 |
} |
187 |
}).center(); |
188 |
} |
189 |
else {
|
190 |
PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false); |
191 |
dlg.center(); |
192 |
permList.updatePermissionTable(); |
193 |
} |
194 |
} |
195 |
}); |
196 |
add.addStyleName("button");
|
197 |
permButtons.add(add); |
198 |
permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER); |
199 |
|
200 |
permButtons.setSpacing(8);
|
201 |
permButtons.addStyleName("pithos-TabPanelBottom");
|
202 |
permPanel.add(permButtons); |
203 |
|
204 |
final Label readForAllNote = new Label("When this option is enabled, the file will be readable" + |
205 |
" by everyone. By checking this option, you are certifying that you have the right to " +
|
206 |
"distribute this file and that it does not violate the Terms of Use.", true); |
207 |
readForAllNote.setStylePrimaryName("pithos-readForAllNote");
|
208 |
|
209 |
pathPanel = new HorizontalPanel();
|
210 |
pathPanel.setVisible(false);
|
211 |
pathPanel.setWidth("100%");
|
212 |
pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); |
213 |
pathPanel.add(new Label("Link")); |
214 |
pathPanel.setSpacing(8);
|
215 |
pathPanel.addStyleName("pithos-TabPanelBottom");
|
216 |
|
217 |
path = new TextBox();
|
218 |
path.setWidth("100%");
|
219 |
path.addClickHandler(new ClickHandler() {
|
220 |
@Override
|
221 |
public void onClick(ClickEvent event) { |
222 |
Pithos.enableIESelection(); |
223 |
((TextBox) event.getSource()).selectAll(); |
224 |
Pithos.preventIESelection(); |
225 |
} |
226 |
}); |
227 |
path.setText(Window.Location.getHost() + file.getPublicUri());
|
228 |
path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
|
229 |
path.setWidth("100%");
|
230 |
path.setReadOnly(true);
|
231 |
pathPanel.add(path); |
232 |
permPanel.add(pathPanel); |
233 |
|
234 |
Scheduler.get().scheduleDeferred(new Command() {
|
235 |
|
236 |
@Override
|
237 |
public void execute() { |
238 |
showLinkIfShared(); |
239 |
} |
240 |
}); |
241 |
return permPanel;
|
242 |
} |
243 |
|
244 |
void showLinkIfShared() {
|
245 |
if (file.isShared()) {
|
246 |
UrlBuilder b = Window.Location.createUrlBuilder();
|
247 |
b.setPath(app.getApiPath() + file.getOwner() + file.getUri()); |
248 |
String href = Window.Location.getHref(); |
249 |
boolean hasParameters = href.contains("?"); |
250 |
path.setText(href + (hasParameters ? "&" : "?") + "goto=" + b.buildString()); |
251 |
pathPanel.setVisible(true);
|
252 |
} |
253 |
else {
|
254 |
pathPanel.setVisible(false);
|
255 |
} |
256 |
} |
257 |
/**
|
258 |
* Accepts any change and updates the file
|
259 |
*
|
260 |
*/
|
261 |
@Override
|
262 |
protected boolean accept() { |
263 |
updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", permList.getPermissions());
|
264 |
return true; |
265 |
} |
266 |
|
267 |
protected void updateMetaData(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) { |
268 |
if (newPermissions != null) { |
269 |
PostRequest updateFile = new PostRequest(api, owner, path) {
|
270 |
@Override
|
271 |
public void onSuccess(Resource result) { |
272 |
HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwner(), path, file) { |
273 |
|
274 |
@Override
|
275 |
public void onSuccess(File _result) { |
276 |
showLinkIfShared(); |
277 |
if (!app.isMySharedSelected())
|
278 |
app.updateFolder(file.getParent(), true, new Command() { |
279 |
|
280 |
@Override
|
281 |
public void execute() { |
282 |
app.updateMySharedRoot(); |
283 |
} |
284 |
}, true);
|
285 |
else
|
286 |
app.updateSharedFolder(file.getParent(), true);
|
287 |
} |
288 |
|
289 |
@Override
|
290 |
public void onError(Throwable t) { |
291 |
GWT.log("", t);
|
292 |
app.setError(t); |
293 |
app.displayError("System error modifying file:" + t.getMessage());
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
protected void onUnauthorized(Response response) { |
298 |
app.sessionExpired(); |
299 |
} |
300 |
}; |
301 |
headFile.setHeader("X-Auth-Token", app.getToken());
|
302 |
Scheduler.get().scheduleDeferred(headFile); |
303 |
} |
304 |
|
305 |
@Override
|
306 |
public void onError(Throwable t) { |
307 |
GWT.log("", t);
|
308 |
app.setError(t); |
309 |
app.displayError("System error modifying file:" + t.getMessage());
|
310 |
} |
311 |
|
312 |
@Override
|
313 |
protected void onUnauthorized(Response response) { |
314 |
app.sessionExpired(); |
315 |
} |
316 |
}; |
317 |
updateFile.setHeader("X-Auth-Token", app.getToken());
|
318 |
|
319 |
String readPermHeader = "read="; |
320 |
String writePermHeader = "write="; |
321 |
for (String u : newPermissions.keySet()) { |
322 |
Boolean[] p = newPermissions.get(u); |
323 |
if (p[0] != null && p[0]) |
324 |
readPermHeader += u + ",";
|
325 |
if (p[1] != null && p[1]) |
326 |
writePermHeader += u + ",";
|
327 |
} |
328 |
if (readPermHeader.endsWith("=")) |
329 |
readPermHeader = "";
|
330 |
else if (readPermHeader.endsWith(",")) |
331 |
readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1); |
332 |
if (writePermHeader.endsWith("=")) |
333 |
writePermHeader = "";
|
334 |
else if (writePermHeader.endsWith(",")) |
335 |
writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1); |
336 |
String permHeader = readPermHeader + ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? ";" : "") + writePermHeader; |
337 |
if (permHeader.length() == 0) |
338 |
permHeader="~";
|
339 |
else
|
340 |
permHeader = URL.encodePathSegment(permHeader);
|
341 |
updateFile.setHeader("X-Object-Sharing", permHeader);
|
342 |
Scheduler.get().scheduleDeferred(updateFile); |
343 |
} |
344 |
else if (!app.isMySharedSelected()) |
345 |
app.updateFolder(file.getParent(), true, new Command() { |
346 |
|
347 |
@Override
|
348 |
public void execute() { |
349 |
if (file.isSharedOrPublished())
|
350 |
app.updateMySharedRoot(); |
351 |
} |
352 |
}, true);
|
353 |
else
|
354 |
app.updateSharedFolder(file.getParent(), true);
|
355 |
} |
356 |
|
357 |
@Override
|
358 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
359 |
super.onPreviewNativeEvent(preview);
|
360 |
|
361 |
NativeEvent evt = preview.getNativeEvent(); |
362 |
if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER) |
363 |
closeDialog(); |
364 |
} |
365 |
} |