Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.7 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.rest.HeadRequest;
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.dom.client.NativeEvent;
46
import com.google.gwt.event.dom.client.ClickEvent;
47
import com.google.gwt.event.dom.client.ClickHandler;
48
import com.google.gwt.event.dom.client.KeyCodes;
49
import com.google.gwt.http.client.Response;
50
import com.google.gwt.http.client.URL;
51
import com.google.gwt.http.client.UrlBuilder;
52
import com.google.gwt.resources.client.ImageResource;
53
import com.google.gwt.user.client.Command;
54
import com.google.gwt.user.client.Window;
55
import com.google.gwt.user.client.Event.NativePreviewEvent;
56
import com.google.gwt.user.client.ui.Anchor;
57
import com.google.gwt.user.client.ui.Button;
58
import com.google.gwt.user.client.ui.FocusPanel;
59
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
60
import com.google.gwt.user.client.ui.HorizontalPanel;
61
import com.google.gwt.user.client.ui.Label;
62
import com.google.gwt.user.client.ui.TextBox;
63
import com.google.gwt.user.client.ui.VerticalPanel;
64

    
65
/**
66
 * The 'File properties' dialog box implementation.
67
 *
68
 */
69
public class FilePermissionsDialog extends AbstractPropertiesDialog {
70

    
71
        protected PermissionsList permList;
72

    
73
        private HorizontalPanel pathPanel;
74
        
75
        private TextBox path;
76
        
77
        /**
78
         * An image bundle for this widgets images.
79
         */
80
        public interface Images extends MessagePanel.Images {
81

    
82
                @Source("gr/grnet/pithos/resources/edit_user.png")
83
                ImageResource permUser();
84

    
85
                @Source("gr/grnet/pithos/resources/groups22.png")
86
                ImageResource permGroup();
87

    
88
                @Source("gr/grnet/pithos/resources/delete.gif")
89
                ImageResource delete();
90
        }
91

    
92
        final File file;
93

    
94
    Images images = GWT.create(Images.class);
95

    
96
        /**
97
         * The widget's constructor.
98
         */
99
        public FilePermissionsDialog(Pithos _app, File _file) {
100
        super(_app);
101
        file = _file;
102

    
103
                Anchor close = new Anchor("close");
104
                close.addStyleName("close");
105
                close.addClickHandler(new ClickHandler() {
106
                        
107
                        @Override
108
                        public void onClick(ClickEvent event) {
109
                                hide();
110
                        }
111
                });
112
                // Set the dialog's caption.
113
                setText("File permissions");
114
                setGlassEnabled(true);
115
                setStyleName("pithos-DialogBox");
116

    
117
                // Outer contains inner and buttons.
118
                final VerticalPanel outer = new VerticalPanel();
119
                outer.add(close);
120
                final FocusPanel focusPanel = new FocusPanel(outer);
121
                // Inner contains generalPanel and permPanel.
122
                inner = new VerticalPanel();
123
                inner.addStyleName("inner");
124

    
125
        inner.add(createSharingPanel());
126

    
127
        outer.add(inner);
128

    
129
                final Button ok = new Button("OK", new ClickHandler() {
130
                        @Override
131
                        public void onClick(ClickEvent event) {
132
                                closeDialog();
133
                        }
134
                });
135
                ok.addStyleName("button");
136

    
137
        outer.add(ok);
138
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
139

    
140
        focusPanel.setFocus(true);
141
        setWidget(outer);
142
        }
143

    
144
    private VerticalPanel createSharingPanel() {
145
        VerticalPanel permPanel = new VerticalPanel();
146

    
147
        permList = new PermissionsList(app, images, file.getPermissions(), file.getOwnerID(), false, new Command() {
148
                        
149
                        @Override
150
                        public void execute() {
151
                                accept();
152
                        }
153
                });
154
        permPanel.add(permList);
155

    
156
        HorizontalPanel permButtons = new HorizontalPanel();
157
        final Button addUser = new Button("Add User", new ClickHandler() {
158
            @Override
159
            public void onClick(ClickEvent event) {
160
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
161
                dlg.center();
162
                permList.updatePermissionTable();
163
            }
164
        });
165
        addUser.addStyleName("button");
166
        permButtons.add(addUser);
167
        permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
168

    
169
        Button add = new Button("Add Group", new ClickHandler() {
170
            @Override
171
            public void onClick(ClickEvent event) {
172
                    if (app.getAccount().getGroups().isEmpty()) {
173
                    new GroupCreateDialog(app, new Command() {
174
                                                
175
                                                @Override
176
                                                public void execute() {
177
                                            if (app.getAccount().getGroups().isEmpty())
178
                                                    return;
179
                                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
180
                                        dlg.center();
181
                                        permList.updatePermissionTable();
182
                                                }
183
                                        }).center();
184
                    }
185
                    else {
186
                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
187
                        dlg.center();
188
                        permList.updatePermissionTable();
189
                    }
190
            }
191
        });
192
        add.addStyleName("button");
193
        permButtons.add(add);
194
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
195

    
196
        permButtons.setSpacing(8);
197
        permButtons.addStyleName("pithos-TabPanelBottom");
198
        permPanel.add(permButtons);
199

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

    
205
        pathPanel = new HorizontalPanel();
206
        pathPanel.setVisible(false);
207
        pathPanel.setWidth("100%");
208
        pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
209
        pathPanel.add(new Label("Link"));
210
        pathPanel.setSpacing(8);
211
        pathPanel.addStyleName("pithos-TabPanelBottom");
212

    
213
        path = new TextBox();
214
        path.setWidth("100%");
215
        path.addClickHandler(new ClickHandler() {
216
            @Override
217
            public void onClick(ClickEvent event) {
218
                Pithos.enableIESelection();
219
                ((TextBox) event.getSource()).selectAll();
220
                Pithos.preventIESelection();
221
            }
222
        });
223
        path.setText(Window.Location.getHost() + file.getPublicUri());
224
        path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
225
        path.setWidth("100%");
226
        path.setReadOnly(true);
227
        pathPanel.add(path);
228
        permPanel.add(pathPanel);
229

    
230
        Scheduler.get().scheduleDeferred(new Command() {
231
                        
232
                        @Override
233
                        public void execute() {
234
                                showLinkIfShared();
235
                        }
236
                });
237
        return permPanel;
238
    }
239

    
240
    void showLinkIfShared() {
241
                if (file.isShared()) {
242
                        UrlBuilder b = Window.Location.createUrlBuilder();
243
                        b.setPath(app.getApiPath() + file.getOwnerID() + file.getUri());
244
                        String href = Window.Location.getHref();
245
                        boolean hasParameters = href.contains("?");
246
                        path.setText(href + (hasParameters ? "&" : "?") + "goto=" + b.buildString());
247
                pathPanel.setVisible(true);
248
                }
249
                else {
250
                        pathPanel.setVisible(false);
251
                }
252
    }
253
        /**
254
         * Accepts any change and updates the file
255
         *
256
         */
257
        @Override
258
        protected boolean accept() {
259
        updateMetaData(app.getApiPath(), app.getUserID(), file.getUri() + "?update=", permList.getPermissions());
260
        return true;
261
        }
262

    
263
        protected void updateMetaData(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) {
264
        if (newPermissions != null) {
265
            PostRequest updateFile = new PostRequest(api, owner, path) {
266
                @Override
267
                public void onSuccess(Resource result) {
268
                        HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwnerID(), path, file) {
269

    
270
                                                @Override
271
                                                public void onSuccess(File _result) {
272
                                                        showLinkIfShared();
273
                                                        if (!app.isMySharedSelected())
274
                                            app.updateFolder(file.getParent(), true, new Command() {
275
                                                                        
276
                                                                        @Override
277
                                                                        public void execute() {
278
                                                                                app.updateMySharedRoot();
279
                                                                        }
280
                                                                }, true);
281
                                                        else
282
                                                                app.updateSharedFolder(file.getParent(), true);
283
                                                }
284

    
285
                                                @Override
286
                                                public void onError(Throwable t) {
287
                                    GWT.log("", t);
288
                                                        app.setError(t);
289
                                    app.displayError("System error modifying file:" + t.getMessage());
290
                                                }
291

    
292
                                                @Override
293
                                                protected void onUnauthorized(Response response) {
294
                                                        app.sessionExpired();
295
                                                }
296
                                        };
297
                                        headFile.setHeader("X-Auth-Token", app.getUserToken());
298
                                        Scheduler.get().scheduleDeferred(headFile);
299
                }
300

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

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

    
353
        @Override
354
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
355
            super.onPreviewNativeEvent(preview);
356

    
357
            NativeEvent evt = preview.getNativeEvent();
358
            if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)
359
                                closeDialog();
360
        }
361
}