Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.3 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

    
122
        inner.add(createSharingPanel());
123

    
124
        outer.add(inner);
125

    
126
                // Create the 'OK' button, along with a listener that hides the dialog
127
                // when the button is clicked.
128
                final Button ok = new Button("OK", new ClickHandler() {
129
                        @Override
130
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
131
                                accept();
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(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);
148
        permPanel.add(permList);
149

    
150
        if (file.getInheritedPermissionsFrom() == null) {
151
            HorizontalPanel permButtons = new HorizontalPanel();
152
            Button add = new Button("Add Group", new ClickHandler() {
153
                @Override
154
                public void onClick(@SuppressWarnings("unused") ClickEvent event) {
155
                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
156
                    dlg.center();
157
                    permList.updatePermissionTable();
158
                }
159
            });
160
            add.addStyleName("button");
161
            permButtons.add(add);
162
            permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
163

    
164
            final Button addUser = new Button("Add User", new ClickHandler() {
165
                @Override
166
                public void onClick(@SuppressWarnings("unused") ClickEvent event) {
167
                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
168
                    dlg.center();
169
                    permList.updatePermissionTable();
170
                }
171
            });
172
            addUser.addStyleName("button");
173
            permButtons.add(addUser);
174
            permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
175

    
176
            permButtons.setSpacing(8);
177
            permButtons.addStyleName("pithos-TabPanelBottom");
178
            permPanel.add(permButtons);
179
        }
180

    
181
        final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
182
                    " by everyone. By checking this option, you are certifying that you have the right to " +
183
                    "distribute this file and that it does not violate the Terms of Use.", true);
184
        readForAllNote.setVisible(false);
185
        readForAllNote.setStylePrimaryName("pithos-readForAllNote");
186

    
187
        readForAll = new CheckBox();
188
        readForAll.setValue(file.isPublished());
189
        readForAll.addClickHandler(new ClickHandler() {
190
            @Override
191
            public void onClick(@SuppressWarnings("unused") ClickEvent event) {
192
                readForAllNote.setVisible(readForAll.getValue());
193
            }
194
        });
195

    
196
        // Only show the read for all permission if the user is the owner.
197
        if (file.getOwner().equals(app.getUsername())) {
198
            final HorizontalPanel permForAll = new HorizontalPanel();
199
            permForAll.add(new Label("Public"));
200
            permForAll.add(readForAll);
201
            permForAll.setSpacing(8);
202
            permForAll.addStyleName("pithos-TabPanelBottom");
203
            permForAll.add(readForAllNote);
204
            permPanel.add(permForAll);
205
        }
206

    
207
        if (file.isPublished()) {
208
            final HorizontalPanel pathPanel = new HorizontalPanel();
209
            pathPanel.setWidth("100%");
210
            pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
211
            pathPanel.add(new Label("Link"));
212
            pathPanel.setSpacing(8);
213
            pathPanel.addStyleName("pithos-TabPanelBottom");
214

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

    
233
        return permPanel;
234
    }
235

    
236
        /**
237
         * Accepts any change and updates the file
238
         *
239
         */
240
        @Override
241
        protected void accept() {
242
                final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
243

    
244
                //only update the read for all perm if the user is the owner
245
        Boolean published = null;
246
                if (readForAll.getValue() != file.isPublished())
247
                        if (file.getOwner().equals(app.getUsername()))
248
                published = readForAll.getValue();
249
        final Boolean finalPublished = published;
250

    
251
        updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", finalPublished, perms);
252
        }
253

    
254
        protected void updateMetaData(String api, String owner, String path, final Boolean published, final Map<String, Boolean[]> newPermissions) {
255
        if (published != null || newPermissions != null) {
256
            PostRequest updateFile = new PostRequest(api, owner, path) {
257
                @Override
258
                public void onSuccess(@SuppressWarnings("unused") Resource result) {
259
                    app.updateFolder(file.getParent(), true, new Command() {
260
                                                
261
                                                @Override
262
                                                public void execute() {
263
                                                        app.updateMySharedRoot();
264
                                                }
265
                                        });
266
                }
267

    
268
                @Override
269
                public void onError(Throwable t) {
270
                    GWT.log("", t);
271
                                        app.setError(t);
272
                    app.displayError("System error modifying file:" + t.getMessage());
273
                }
274

    
275
                                @Override
276
                                protected void onUnauthorized(@SuppressWarnings("unused") Response response) {
277
                                        app.sessionExpired();
278
                                }
279
            };
280
            updateFile.setHeader("X-Auth-Token", app.getToken());
281
            
282
            if (published != null)
283
                updateFile.setHeader("X-Object-Public", published.toString());
284
            if (newPermissions != null) {
285
                String readPermHeader = "read=";
286
                String writePermHeader = "write=";
287
                for (String u : newPermissions.keySet()) {
288
                    Boolean[] p = newPermissions.get(u);
289
                    if (p[0] != null && p[0])
290
                        readPermHeader += u + ",";
291
                    if (p[1] != null && p[1])
292
                        writePermHeader += u + ",";
293
                }
294
                if (readPermHeader.endsWith("="))
295
                    readPermHeader = "";
296
                else if (readPermHeader.endsWith(","))
297
                    readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
298
                if (writePermHeader.endsWith("="))
299
                    writePermHeader = "";
300
                else if (writePermHeader.endsWith(","))
301
                    writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
302
                String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
303
                if (permHeader.length() == 0)
304
                    permHeader="~";
305
                else
306
                        permHeader = URL.encodePathSegment(permHeader);
307
                updateFile.setHeader("X-Object-Sharing", permHeader);
308
            }
309
            Scheduler.get().scheduleDeferred(updateFile);
310
        }
311
        else
312
            app.updateFolder(file.getParent(), true, new Command() {
313
                                
314
                                @Override
315
                                public void execute() {
316
                                        if (file.isShared())
317
                                                app.updateMySharedRoot();
318
                                }
319
                        });
320
    }
321
}