Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.6 kB)

1
/*
2
 * Copyright 2011-2013 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 com.google.gwt.core.client.GWT;
38
import com.google.gwt.core.client.Scheduler;
39
import com.google.gwt.dom.client.NativeEvent;
40
import com.google.gwt.event.dom.client.ClickEvent;
41
import com.google.gwt.event.dom.client.ClickHandler;
42
import com.google.gwt.event.dom.client.KeyCodes;
43
import com.google.gwt.http.client.Response;
44
import com.google.gwt.http.client.URL;
45
import com.google.gwt.http.client.UrlBuilder;
46
import com.google.gwt.resources.client.ImageResource;
47
import com.google.gwt.user.client.Command;
48
import com.google.gwt.user.client.Event.NativePreviewEvent;
49
import com.google.gwt.user.client.Window;
50
import com.google.gwt.user.client.ui.*;
51
import gr.grnet.pithos.web.client.foldertree.File;
52
import gr.grnet.pithos.web.client.rest.HeadRequest;
53
import gr.grnet.pithos.web.client.rest.PostRequest;
54

    
55
import java.util.Map;
56

    
57
/**
58
 * The 'File properties' dialog box implementation.
59
 *
60
 */
61
public class FilePermissionsDialog extends AbstractPropertiesDialog {
62

    
63
        protected PermissionsList permList;
64

    
65
        private HorizontalPanel pathPanel;
66
        
67
        private TextBox path;
68
        
69
        /**
70
         * An image bundle for this widgets images.
71
         */
72
        public interface Images extends MessagePanel.Images {
73

    
74
                @Source("gr/grnet/pithos/resources/edit_user.png")
75
                ImageResource permUser();
76

    
77
                @Source("gr/grnet/pithos/resources/groups22.png")
78
                ImageResource permGroup();
79

    
80
                @Source("gr/grnet/pithos/resources/delete.gif")
81
                ImageResource delete();
82
        }
83

    
84
        final File file;
85

    
86
    FileShareDialog.PrivateSharingImages images = GWT.create(FileShareDialog.PrivateSharingImages.class);
87

    
88
        /**
89
         * The widget's constructor.
90
         */
91
        public FilePermissionsDialog(Pithos _app, File _file) {
92
        super(_app);
93
        file = _file;
94

    
95
                Anchor close = new Anchor("close");
96
                close.addStyleName("close");
97
                close.addClickHandler(new ClickHandler() {
98
                        
99
                        @Override
100
                        public void onClick(ClickEvent event) {
101
                                hide();
102
                        }
103
                });
104
                // Set the dialog's caption.
105
                setText("File permissions");
106
                setGlassEnabled(true);
107
                setStyleName("pithos-DialogBox");
108

    
109
                // Outer contains inner and buttons.
110
                final VerticalPanel outer = new VerticalPanel();
111
                outer.add(close);
112
                final FocusPanel focusPanel = new FocusPanel(outer);
113
                // Inner contains generalPanel and permPanel.
114
                inner = new VerticalPanel();
115
                inner.addStyleName("inner");
116

    
117
        inner.add(createSharingPanel());
118

    
119
        outer.add(inner);
120

    
121
                final Button ok = new Button("OK", new ClickHandler() {
122
                        @Override
123
                        public void onClick(ClickEvent event) {
124
                                closeDialog();
125
                        }
126
                });
127
                ok.addStyleName("button");
128

    
129
        outer.add(ok);
130
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
131

    
132
        focusPanel.setFocus(true);
133
        setWidget(outer);
134
        }
135

    
136
    private VerticalPanel createSharingPanel() {
137
        VerticalPanel permPanel = new VerticalPanel();
138

    
139
        permList = new PermissionsList(app, images, file.getPermissions(), file.getOwnerID(), false, new Command() {
140
                        
141
                        @Override
142
                        public void execute() {
143
                                accept();
144
                        }
145
                });
146
        permPanel.add(permList);
147

    
148
        HorizontalPanel permButtons = new HorizontalPanel();
149
        final Button addUser = new Button("Add User", new ClickHandler() {
150
            @Override
151
            public void onClick(ClickEvent event) {
152
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
153
                dlg.center();
154
                permList.updatePermissionTable();
155
            }
156
        });
157
        addUser.addStyleName("button");
158
        permButtons.add(addUser);
159
        permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
160

    
161
        Button add = new Button("Add Group", new ClickHandler() {
162
            @Override
163
            public void onClick(ClickEvent event) {
164
                    if (app.getAccount().getGroups().isEmpty()) {
165
                    new GroupCreateDialog(app, new Command() {
166
                                                
167
                                                @Override
168
                                                public void execute() {
169
                                            if (app.getAccount().getGroups().isEmpty())
170
                                                    return;
171
                                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
172
                                        dlg.center();
173
                                        permList.updatePermissionTable();
174
                                                }
175
                                        }).center();
176
                    }
177
                    else {
178
                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
179
                        dlg.center();
180
                        permList.updatePermissionTable();
181
                    }
182
            }
183
        });
184
        add.addStyleName("button");
185
        permButtons.add(add);
186
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
187

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

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

    
197
        pathPanel = new HorizontalPanel();
198
        pathPanel.setVisible(false);
199
        pathPanel.setWidth(Const.PERCENT_100);
200
        pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
201
        pathPanel.add(new Label("Link"));
202
        pathPanel.setSpacing(8);
203
        pathPanel.addStyleName("pithos-TabPanelBottom");
204

    
205
        path = new TextBox();
206
        path.setWidth(Const.PERCENT_100);
207
        path.addClickHandler(new ClickHandler() {
208
            @Override
209
            public void onClick(ClickEvent event) {
210
                Pithos.enableIESelection();
211
                ((TextBox) event.getSource()).selectAll();
212
                Pithos.preventIESelection();
213
            }
214
        });
215
        path.setText(Window.Location.getHost() + file.getPublicUri());
216
        path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
217
        path.setWidth(Const.PERCENT_100);
218
        path.setReadOnly(true);
219
        pathPanel.add(path);
220
        permPanel.add(pathPanel);
221

    
222
        Scheduler.get().scheduleDeferred(new Command() {
223
                        
224
                        @Override
225
                        public void execute() {
226
                                showLinkIfShared();
227
                        }
228
                });
229
        return permPanel;
230
    }
231

    
232
    void showLinkIfShared() {
233
                if (file.isShared()) {
234
                        UrlBuilder b = Window.Location.createUrlBuilder();
235
                        b.setPath(Pithos.getStorageAPIURL() + file.getOwnerID() + file.getUri());
236
                        path.setText(b.buildString());
237
                pathPanel.setVisible(true);
238
                }
239
                else {
240
                        pathPanel.setVisible(false);
241
                }
242
    }
243
        /**
244
         * Accepts any change and updates the file
245
         *
246
         */
247
        @Override
248
        protected boolean accept() {
249
        updateMetaData(
250
            Pithos.getStorageAPIURL(),
251
            app.getUserID(),
252
            file.getUri() + Const.QUESTION_MARK_UPDATE_EQ,
253
            permList.getPermissions()
254
        );
255
        return true;
256
        }
257

    
258
        protected void updateMetaData(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) {
259
        if (newPermissions != null) {
260
            PostRequest updateFile = new PostRequest(api, owner, path) {
261
                @Override
262
                public void onSuccess(Resource result) {
263
                        HeadRequest<File> headFile = new HeadRequest<File>(File.class, Pithos.getStorageAPIURL(), file.getOwnerID(), path, file) {
264

    
265
                                                @Override
266
                                                public void onSuccess(File _result) {
267
                                                        showLinkIfShared();
268
                                                        if (!app.isMySharedSelected())
269
                                            app.updateFolder(file.getParent(), true, new Command() {
270
                                                                        
271
                                                                        @Override
272
                                                                        public void execute() {
273
                                                                                app.updateMySharedRoot();
274
                                                                        }
275
                                                                }, true);
276
                                                        else
277
                                                                app.updateSharedFolder(file.getParent(), true);
278
                                                }
279

    
280
                                                @Override
281
                                                public void onError(Throwable t) {
282
                                                        app.setError(t);
283
                                    app.displayError("System error modifying file:" + t.getMessage());
284
                                                }
285

    
286
                                                @Override
287
                                                protected void onUnauthorized(Response response) {
288
                                                        app.sessionExpired();
289
                                                }
290
                                        };
291
                                        headFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
292
                                        Scheduler.get().scheduleDeferred(headFile);
293
                }
294

    
295
                @Override
296
                public void onError(Throwable t) {
297
                                        app.setError(t);
298
                    app.displayError("System error modifying file:" + t.getMessage());
299
                }
300

    
301
                                @Override
302
                                protected void onUnauthorized(Response response) {
303
                                        app.sessionExpired();
304
                                }
305
            };
306
            updateFile.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
307
            
308
            String readPermHeader = Const.READ_EQ;
309
            String writePermHeader = Const.WRITE_EQ;
310
            for (String u : newPermissions.keySet()) {
311
                Boolean[] p = newPermissions.get(u);
312
                if (p[0] != null && p[0]) {
313
                    readPermHeader += u + Const.COMMA;
314
                }
315
                if (p[1] != null && p[1]) {
316
                    writePermHeader += u + Const.COMMA;
317
                }
318
            }
319
            if (readPermHeader.endsWith(Const.EQ)) {
320
                readPermHeader = "";
321
            }
322
            else if (readPermHeader.endsWith(Const.COMMA)) {
323
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
324
            }
325
            if (writePermHeader.endsWith(Const.EQ)) {
326
                writePermHeader = "";
327
            }
328
            else if (writePermHeader.endsWith(Const.COMMA)) {
329
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
330
            }
331
            String permHeader = readPermHeader +
332
                ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  Const.SEMI : "") + writePermHeader;
333
            if (permHeader.length() == 0) {
334
                permHeader=Const.TILDE;
335
            }
336
            else {
337
                    permHeader = URL.encodePathSegment(permHeader);
338
            }
339
            updateFile.setHeader(Const.X_OBJECT_SHARING, permHeader);
340
            Scheduler.get().scheduleDeferred(updateFile);
341
        }
342
        else if (!app.isMySharedSelected()) {
343
            app.updateFolder(file.getParent(), true, new Command() {
344
                                @Override
345
                                public void execute() {
346
                                        if (file.isSharedOrPublished())
347
                                                app.updateMySharedRoot();
348
                                }
349
                        }, true);
350
        }
351
        else {
352
                app.updateSharedFolder(file.getParent(), true);
353
        }
354
    }
355

    
356
        @Override
357
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
358
            super.onPreviewNativeEvent(preview);
359

    
360
            NativeEvent evt = preview.getNativeEvent();
361
            if (evt.getType().equals(Const.EVENT_TYPE_KEYDOWN) &&
362
            evt.getKeyCode() == KeyCodes.KEY_ENTER) {
363

    
364
            closeDialog();
365
        }
366
        }
367
}