Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.9 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.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(), false);
147
        permPanel.add(permList);
148

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

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

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

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

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

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

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

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

    
230
        return permPanel;
231
    }
232

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

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

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

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

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

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