Statistics
| Branch: | Tag: | Revision:

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

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

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

    
129
        inner.add(createSharingPanel());
130

    
131
        outer.add(inner);
132

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

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

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

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

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

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

    
173
        Button add = new Button("Add Group", new ClickHandler() {
174
            @Override
175
            public void onClick(ClickEvent event) {
176
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
177
                dlg.center();
178
                permList.updatePermissionTable();
179
            }
180
        });
181
        add.addStyleName("button");
182
        permButtons.add(add);
183
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
184

    
185
        permButtons.setSpacing(8);
186
        permButtons.addStyleName("pithos-TabPanelBottom");
187
        permPanel.add(permButtons);
188

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

    
194
        pathPanel = new HorizontalPanel();
195
        pathPanel.setVisible(false);
196
        pathPanel.setWidth("100%");
197
        pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
198
        pathPanel.add(new Label("Link"));
199
        pathPanel.setSpacing(8);
200
        pathPanel.addStyleName("pithos-TabPanelBottom");
201

    
202
        path = new TextBox();
203
        path.setWidth("100%");
204
        path.addClickHandler(new ClickHandler() {
205
            @Override
206
            public void onClick(ClickEvent event) {
207
                Pithos.enableIESelection();
208
                ((TextBox) event.getSource()).selectAll();
209
                Pithos.preventIESelection();
210
            }
211
        });
212
        path.setText(Window.Location.getHost() + file.getPublicUri());
213
        path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
214
        path.setWidth("100%");
215
        path.setReadOnly(true);
216
        pathPanel.add(path);
217
        permPanel.add(pathPanel);
218

    
219
        Scheduler.get().scheduleDeferred(new Command() {
220
                        
221
                        @Override
222
                        public void execute() {
223
                                showLinkIfShared();
224
                        }
225
                });
226
        return permPanel;
227
    }
228

    
229
    void showLinkIfShared() {
230
                if (file.isShared()) {
231
                        UrlBuilder b = Window.Location.createUrlBuilder();
232
                        b.setPath(app.getApiPath() + file.getOwner() + file.getUri());
233
                        String href = Window.Location.getHref();
234
                        boolean hasParameters = href.contains("?");
235
                        path.setText(href + (hasParameters ? "&" : "?") + "goto=" + b.buildString());
236
                pathPanel.setVisible(true);
237
                }
238
                else {
239
                        pathPanel.setVisible(false);
240
                }
241
    }
242
        /**
243
         * Accepts any change and updates the file
244
         *
245
         */
246
        @Override
247
        protected void accept() {
248
        updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", permList.getPermissions());
249
        }
250

    
251
        protected void updateMetaData(String api, String owner, final String path, final Map<String, Boolean[]> newPermissions) {
252
        if (newPermissions != null) {
253
            PostRequest updateFile = new PostRequest(api, owner, path) {
254
                @Override
255
                public void onSuccess(Resource result) {
256
                        HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwner(), path, file) {
257

    
258
                                                @Override
259
                                                public void onSuccess(File _result) {
260
                                                        showLinkIfShared();
261
                                    app.updateFolder(file.getParent(), true, new Command() {
262
                                                                
263
                                                                @Override
264
                                                                public void execute() {
265
                                                                        app.updateMySharedRoot();
266
                                                                }
267
                                                        }, true);
268
                                                }
269

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

    
277
                                                @Override
278
                                                protected void onUnauthorized(Response response) {
279
                                                        app.sessionExpired();
280
                                                }
281
                                        };
282
                                        headFile.setHeader("X-Auth-Token", app.getToken());
283
                                        Scheduler.get().scheduleDeferred(headFile);
284
                }
285

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

    
293
                                @Override
294
                                protected void onUnauthorized(Response response) {
295
                                        app.sessionExpired();
296
                                }
297
            };
298
            updateFile.setHeader("X-Auth-Token", app.getToken());
299
            
300
            String readPermHeader = "read=";
301
            String writePermHeader = "write=";
302
            for (String u : newPermissions.keySet()) {
303
                Boolean[] p = newPermissions.get(u);
304
                if (p[0] != null && p[0])
305
                    readPermHeader += u + ",";
306
                if (p[1] != null && p[1])
307
                    writePermHeader += u + ",";
308
            }
309
            if (readPermHeader.endsWith("="))
310
                readPermHeader = "";
311
            else if (readPermHeader.endsWith(","))
312
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
313
            if (writePermHeader.endsWith("="))
314
                writePermHeader = "";
315
            else if (writePermHeader.endsWith(","))
316
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
317
            String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
318
            if (permHeader.length() == 0)
319
                permHeader="~";
320
            else
321
                    permHeader = URL.encodePathSegment(permHeader);
322
            updateFile.setHeader("X-Object-Sharing", permHeader);
323
            Scheduler.get().scheduleDeferred(updateFile);
324
        }
325
        else
326
            app.updateFolder(file.getParent(), true, new Command() {
327
                                
328
                                @Override
329
                                public void execute() {
330
                                        if (file.isSharedOrPublished())
331
                                                app.updateMySharedRoot();
332
                                }
333
                        }, true);
334
    }
335

    
336
        @Override
337
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
338
            super.onPreviewNativeEvent(preview);
339

    
340
            NativeEvent evt = preview.getNativeEvent();
341
            if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)
342
                                closeDialog();
343
        }
344
}