Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.8 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("close");
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
                setGlassEnabled(true);
118
                setStyleName("pithos-DialogBox");
119

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

    
128
        inner.add(createSharingPanel());
129

    
130
        outer.add(inner);
131

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

    
140
        outer.add(ok);
141
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
142

    
143
        focusPanel.setFocus(true);
144
        setWidget(outer);
145
        }
146

    
147
    private VerticalPanel createSharingPanel() {
148
        VerticalPanel permPanel = new VerticalPanel();
149

    
150
        permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), false, new Command() {
151
                        
152
                        @Override
153
                        public void execute() {
154
                                accept();
155
                        }
156
                });
157
        permPanel.add(permList);
158

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

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

    
199
        permButtons.setSpacing(8);
200
        permButtons.addStyleName("pithos-TabPanelBottom");
201
        permPanel.add(permButtons);
202

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

    
208
        pathPanel = new HorizontalPanel();
209
        pathPanel.setVisible(false);
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
        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
        Scheduler.get().scheduleDeferred(new Command() {
234
                        
235
                        @Override
236
                        public void execute() {
237
                                showLinkIfShared();
238
                        }
239
                });
240
        return permPanel;
241
    }
242

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

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

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

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

    
295
                                                @Override
296
                                                protected void onUnauthorized(Response response) {
297
                                                        app.sessionExpired();
298
                                                }
299
                                        };
300
                                        headFile.setHeader("X-Auth-Token", app.getToken());
301
                                        Scheduler.get().scheduleDeferred(headFile);
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
            updateFile.setHeader("X-Auth-Token", app.getToken());
317
            
318
            String readPermHeader = "read=";
319
            String writePermHeader = "write=";
320
            for (String u : newPermissions.keySet()) {
321
                Boolean[] p = newPermissions.get(u);
322
                if (p[0] != null && p[0])
323
                    readPermHeader += u + ",";
324
                if (p[1] != null && p[1])
325
                    writePermHeader += u + ",";
326
            }
327
            if (readPermHeader.endsWith("="))
328
                readPermHeader = "";
329
            else if (readPermHeader.endsWith(","))
330
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
331
            if (writePermHeader.endsWith("="))
332
                writePermHeader = "";
333
            else if (writePermHeader.endsWith(","))
334
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
335
            String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
336
            if (permHeader.length() == 0)
337
                permHeader="~";
338
            else
339
                    permHeader = URL.encodePathSegment(permHeader);
340
            updateFile.setHeader("X-Object-Sharing", permHeader);
341
            Scheduler.get().scheduleDeferred(updateFile);
342
        }
343
        else if (!app.isMySharedSelected())
344
            app.updateFolder(file.getParent(), true, new Command() {
345
                                
346
                                @Override
347
                                public void execute() {
348
                                        if (file.isSharedOrPublished())
349
                                                app.updateMySharedRoot();
350
                                }
351
                        }, true);
352
        else
353
                app.updateSharedFolder(file.getParent(), true);
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("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)
362
                                closeDialog();
363
        }
364
}