Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.1 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.FocusPanel;
56
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
57
import com.google.gwt.user.client.ui.HorizontalPanel;
58
import com.google.gwt.user.client.ui.Label;
59
import com.google.gwt.user.client.ui.TextBox;
60
import com.google.gwt.user.client.ui.VerticalPanel;
61

    
62
/**
63
 * The 'File properties' dialog box implementation.
64
 *
65
 */
66
public class FilePermissionsDialog extends AbstractPropertiesDialog {
67

    
68
        protected PermissionsList permList;
69

    
70
        protected CheckBox readForAll;
71

    
72
        /**
73
         * An image bundle for this widgets images.
74
         */
75
        public interface Images extends MessagePanel.Images {
76

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

    
80
                @Source("gr/grnet/pithos/resources/groups22.png")
81
                ImageResource permGroup();
82

    
83
                @Source("gr/grnet/pithos/resources/editdelete.png")
84
                ImageResource delete();
85
        }
86

    
87
        final File file;
88

    
89
    Images images = GWT.create(Images.class);
90

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

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

    
113
                // Outer contains inner and buttons.
114
                final VerticalPanel outer = new VerticalPanel();
115
                outer.add(close);
116
                final FocusPanel focusPanel = new FocusPanel(outer);
117
                // Inner contains generalPanel and permPanel.
118
                inner = new VerticalPanel();
119
                inner.addStyleName("inner");
120

    
121
        inner.add(createSharingPanel());
122

    
123
        outer.add(inner);
124

    
125
                // Create the 'OK' button, along with a listener that hides the dialog
126
                // when the button is clicked.
127
                final Button ok = new Button("OK", new ClickHandler() {
128
                        @Override
129
                        public void onClick(ClickEvent event) {
130
                                accept();
131
                                closeDialog();
132
                        }
133
                });
134
                ok.addStyleName("button");
135

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

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

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

    
146
        permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);
147
        permPanel.add(permList);
148

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

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

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

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

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

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

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

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

    
232
        return permPanel;
233
    }
234

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

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

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

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

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

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