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