Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FilePermissionsDialog.java @ 6acd4df3

History | View | Annotate | Download (13.6 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
        protected CheckBox readForAll;
77
        
78
        private HorizontalPanel pathPanel;
79
        
80
        private TextBox path;
81
        
82
        private Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
83
        
84
        /**
85
         * An image bundle for this widgets images.
86
         */
87
        public interface Images extends MessagePanel.Images {
88

    
89
                @Source("gr/grnet/pithos/resources/edit_user.png")
90
                ImageResource permUser();
91

    
92
                @Source("gr/grnet/pithos/resources/groups22.png")
93
                ImageResource permGroup();
94

    
95
                @Source("gr/grnet/pithos/resources/editdelete.png")
96
                ImageResource delete();
97
        }
98

    
99
        final File file;
100

    
101
    Images images = GWT.create(Images.class);
102

    
103
        /**
104
         * The widget's constructor.
105
         */
106
        public FilePermissionsDialog(Pithos _app, File _file) {
107
        super(_app);
108
        file = _file;
109

    
110
                Anchor close = new Anchor();
111
                close.addStyleName("close");
112
                close.addClickHandler(new ClickHandler() {
113
                        
114
                        @Override
115
                        public void onClick(ClickEvent event) {
116
                                hide();
117
                        }
118
                });
119
                // Set the dialog's caption.
120
                setText("File permissions");
121
                setAnimationEnabled(true);
122
                setGlassEnabled(true);
123
                setStyleName("pithos-DialogBox");
124

    
125
                // Outer contains inner and buttons.
126
                final VerticalPanel outer = new VerticalPanel();
127
                outer.add(close);
128
                final FocusPanel focusPanel = new FocusPanel(outer);
129
                // Inner contains generalPanel and permPanel.
130
                inner = new VerticalPanel();
131
                inner.addStyleName("inner");
132

    
133
        inner.add(createSharingPanel());
134

    
135
        outer.add(inner);
136

    
137
                final Button ok = new Button("Close", new ClickHandler() {
138
                        @Override
139
                        public void onClick(ClickEvent event) {
140
                                closeDialog();
141
                        }
142
                });
143
                ok.addStyleName("button");
144

    
145
        outer.add(ok);
146
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
147

    
148
        focusPanel.setFocus(true);
149
        setWidget(outer);
150
        }
151

    
152
    private VerticalPanel createSharingPanel() {
153
        VerticalPanel permPanel = new VerticalPanel();
154

    
155
        permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), false, new Command() {
156
                        
157
                        @Override
158
                        public void execute() {
159
                                accept();
160
                        }
161
                });
162
        permPanel.add(permList);
163

    
164
        HorizontalPanel permButtons = new HorizontalPanel();
165
        final Button addUser = new Button("Add User", new ClickHandler() {
166
            @Override
167
            public void onClick(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
        Button add = new Button("Add Group", new ClickHandler() {
178
            @Override
179
            public void onClick(ClickEvent event) {
180
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
181
                dlg.center();
182
                permList.updatePermissionTable();
183
            }
184
        });
185
        add.addStyleName("button");
186
        permButtons.add(add);
187
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
188

    
189
        permButtons.setSpacing(8);
190
        permButtons.addStyleName("pithos-TabPanelBottom");
191
        permPanel.add(permButtons);
192

    
193
        final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
194
                    " by everyone. By checking this option, you are certifying that you have the right to " +
195
                    "distribute this file and that it does not violate the Terms of Use.", true);
196
        readForAllNote.setStylePrimaryName("pithos-readForAllNote");
197

    
198
        readForAll = new CheckBox();
199
        readForAll.setValue(file.isPublished());
200
        readForAll.addClickHandler(new ClickHandler() {
201
            @Override
202
            public void onClick(ClickEvent event) {
203
                    accept();
204
            }
205
        });
206

    
207
        // Only show the read for all permission if the user is the owner.
208
        if (file.getOwner().equals(app.getUsername())) {
209
            final HorizontalPanel permForAll = new HorizontalPanel();
210
            permForAll.add(new Label("Public"));
211
            permForAll.add(readForAll);
212
            permForAll.setSpacing(8);
213
            permForAll.addStyleName("pithos-TabPanelBottom");
214
            permForAll.add(readForAllNote);
215
            permPanel.add(permForAll);
216
        }
217

    
218
        pathPanel = new HorizontalPanel();
219
        pathPanel.setVisible(false);
220
        pathPanel.setWidth("100%");
221
        pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
222
        pathPanel.add(new Label("Link"));
223
        pathPanel.setSpacing(8);
224
        pathPanel.addStyleName("pithos-TabPanelBottom");
225

    
226
        path = new TextBox();
227
        path.setWidth("100%");
228
        path.addClickHandler(new ClickHandler() {
229
            @Override
230
            public void onClick(ClickEvent event) {
231
                Pithos.enableIESelection();
232
                ((TextBox) event.getSource()).selectAll();
233
                Pithos.preventIESelection();
234
            }
235
        });
236
        path.setText(Window.Location.getHost() + file.getPublicUri());
237
        path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
238
        path.setWidth("100%");
239
        path.setReadOnly(true);
240
        pathPanel.add(path);
241
        permPanel.add(pathPanel);
242

    
243
        Scheduler.get().scheduleDeferred(new Command() {
244
                        
245
                        @Override
246
                        public void execute() {
247
                                showLinkIfPublished();
248
                        }
249
                });
250
        return permPanel;
251
    }
252

    
253
    void showLinkIfPublished() {
254
                if (file.isShared()) {
255
                        UrlBuilder b = Window.Location.createUrlBuilder();
256
                        if (file.isPublished()) {
257
                                b.setPath(file.getPublicUri());
258
                                path.setText(b.buildString());
259
                        }
260
                        else {
261
                                b.setPath(app.getApiPath() + file.getOwner() + file.getUri());
262
                                String href = Window.Location.getHref();
263
                                boolean hasParameters = href.contains("?");
264
                                path.setText(href + (hasParameters ? "&" : "?") + "goto=" + b.buildString());
265
                        }
266
                pathPanel.setVisible(true);
267
                }
268
                else {
269
                        pathPanel.setVisible(false);
270
                }
271
    }
272
        /**
273
         * Accepts any change and updates the file
274
         *
275
         */
276
        @Override
277
        protected void accept() {
278
        Boolean published = null;
279
                if (readForAll.getValue() != file.isPublished())
280
                        if (file.getOwner().equals(app.getUsername()))
281
                published = readForAll.getValue();
282
        updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", published, permList.getPermissions());
283
        }
284

    
285
        protected void updateMetaData(String api, String owner, final String path, final Boolean published, final Map<String, Boolean[]> newPermissions) {
286
        if (published != null || newPermissions != null) {
287
            PostRequest updateFile = new PostRequest(api, owner, path) {
288
                @Override
289
                public void onSuccess(Resource result) {
290
                        HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwner(), path, file) {
291

    
292
                                                @Override
293
                                                public void onSuccess(File _result) {
294
                                                        showLinkIfPublished();
295
                                    app.updateFolder(file.getParent(), true, new Command() {
296
                                                                
297
                                                                @Override
298
                                                                public void execute() {
299
                                                                        app.updateMySharedRoot();
300
                                                                }
301
                                                        }, true);
302
                                                }
303

    
304
                                                @Override
305
                                                public void onError(Throwable t) {
306
                                    GWT.log("", t);
307
                                                        app.setError(t);
308
                                    app.displayError("System error modifying file:" + t.getMessage());
309
                                                }
310

    
311
                                                @Override
312
                                                protected void onUnauthorized(Response response) {
313
                                                        app.sessionExpired();
314
                                                }
315
                                        };
316
                                        headFile.setHeader("X-Auth-Token", app.getToken());
317
                                        Scheduler.get().scheduleDeferred(headFile);
318
                }
319

    
320
                @Override
321
                public void onError(Throwable t) {
322
                    GWT.log("", t);
323
                                        app.setError(t);
324
                    app.displayError("System error modifying file:" + t.getMessage());
325
                }
326

    
327
                                @Override
328
                                protected void onUnauthorized(Response response) {
329
                                        app.sessionExpired();
330
                                }
331
            };
332
            updateFile.setHeader("X-Auth-Token", app.getToken());
333
            
334
            if (published != null)
335
                updateFile.setHeader("X-Object-Public", published.toString());
336
            if (newPermissions != null) {
337
                String readPermHeader = "read=";
338
                String writePermHeader = "write=";
339
                for (String u : newPermissions.keySet()) {
340
                    Boolean[] p = newPermissions.get(u);
341
                    if (p[0] != null && p[0])
342
                        readPermHeader += u + ",";
343
                    if (p[1] != null && p[1])
344
                        writePermHeader += u + ",";
345
                }
346
                if (readPermHeader.endsWith("="))
347
                    readPermHeader = "";
348
                else if (readPermHeader.endsWith(","))
349
                    readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
350
                if (writePermHeader.endsWith("="))
351
                    writePermHeader = "";
352
                else if (writePermHeader.endsWith(","))
353
                    writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
354
                String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
355
                if (permHeader.length() == 0)
356
                    permHeader="~";
357
                else
358
                        permHeader = URL.encodePathSegment(permHeader);
359
                updateFile.setHeader("X-Object-Sharing", permHeader);
360
            }
361
            Scheduler.get().scheduleDeferred(updateFile);
362
        }
363
        else
364
            app.updateFolder(file.getParent(), true, new Command() {
365
                                
366
                                @Override
367
                                public void execute() {
368
                                        if (file.isShared())
369
                                                app.updateMySharedRoot();
370
                                }
371
                        }, true);
372
    }
373

    
374
        @Override
375
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
376
            super.onPreviewNativeEvent(preview);
377

    
378
            NativeEvent evt = preview.getNativeEvent();
379
            if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)
380
                                closeDialog();
381
        }
382
}