Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FolderPermissionsDialog.java @ 64d3b76b

History | View | Annotate | Download (11.9 kB)

1
/*
2
 * Copyright 2011-2013 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.Folder;
38
import gr.grnet.pithos.web.client.rest.PostRequest;
39
import gr.grnet.pithos.web.client.rest.PutRequest;
40
import gr.grnet.pithos.web.client.rest.RestException;
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.event.dom.client.KeyDownEvent;
51
import com.google.gwt.http.client.Response;
52
import com.google.gwt.http.client.URL;
53
import com.google.gwt.user.client.Command;
54
import com.google.gwt.user.client.Event.NativePreviewEvent;
55
import com.google.gwt.user.client.ui.Anchor;
56
import com.google.gwt.user.client.ui.Button;
57
import com.google.gwt.user.client.ui.DialogBox;
58
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
59
import com.google.gwt.user.client.ui.HorizontalPanel;
60
import com.google.gwt.user.client.ui.Label;
61
import com.google.gwt.user.client.ui.VerticalPanel;
62

    
63
/**
64
 * The 'Folder properties' dialog box implementation.
65
 */
66
public class FolderPermissionsDialog extends DialogBox {
67

    
68
    protected Pithos app;
69

    
70
        /**
71
         * The widget that holds the folderName of the folder.
72
         */
73
        Label folderName = new Label();
74

    
75
        protected PermissionsList permList;
76

    
77
        final Folder folder;
78

    
79
        final VerticalPanel inner;
80

    
81
        /**
82
         * The widget's constructor.
83
         */
84
        public FolderPermissionsDialog(final Pithos app, Folder selected) {
85
        this.app = app;
86
                Anchor close = new Anchor("close");
87
                close.addStyleName("close");
88
                close.addClickHandler(new ClickHandler() {
89
                        
90
                        @Override
91
                        public void onClick(ClickEvent event) {
92
                                hide();
93
                        }
94
                });
95

    
96
                setGlassEnabled(true);
97
                setStyleName("pithos-DialogBox");
98

    
99
                // Enable IE selection for the dialog (must disable it upon closing it)
100
                Pithos.enableIESelection();
101

    
102
                folder = selected;
103

    
104
                setText("Folder permissions");
105

    
106
                // Outer contains inner and buttons
107
                VerticalPanel outer = new VerticalPanel();
108
                outer.add(close);
109
                // Inner contains generalPanel and permPanel
110
                inner = new VerticalPanel();
111
                inner.addStyleName("inner");
112

    
113

    
114
                folderName.setText(folder.getName());
115

    
116
        VerticalPanel permPanel = new VerticalPanel();
117
        FileShareDialog.PrivateSharingImages images = GWT.create(FileShareDialog.PrivateSharingImages.class);
118
        permList = new PermissionsList(app, images, folder.getPermissions(), folder.getOwnerID(), false, null);
119
        permPanel.add(permList);
120

    
121
        HorizontalPanel permButtons = new HorizontalPanel();
122
        Button addUser = new Button("Add User", new ClickHandler() {
123
            @Override
124
            public void onClick(ClickEvent event) {
125
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
126
                dlg.center();
127
            }
128
        });
129
        addUser.addStyleName("button");
130
        permButtons.add(addUser);
131
        permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
132

    
133
        Button add = new Button("Add Group", new ClickHandler() {
134
            @Override
135
            public void onClick(ClickEvent event) {
136
                    if (app.getAccount().getGroups().isEmpty()) {
137
                    new GroupCreateDialog(app, new Command() {
138
                                                
139
                                                @Override
140
                                                public void execute() {
141
                                            if (app.getAccount().getGroups().isEmpty())
142
                                                    return;
143
                                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
144
                                        dlg.center();
145
                                                }
146
                                        }).center();
147
                    }
148
                    else {
149
                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
150
                        dlg.center();
151
                    }
152
            }
153
        });
154
        add.addStyleName("button");
155
        permButtons.add(add);
156
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
157

    
158
        permButtons.setSpacing(8);
159
        permPanel.add(permButtons);
160

    
161
        inner.add(permPanel);
162

    
163
        outer.add(inner);
164

    
165
                // Create the 'Create/Update' button, along with a listener that hides the dialog
166
                // when the button is clicked and quits the application.
167
                String okLabel = "Update";
168
                final Button ok = new Button(okLabel, new ClickHandler() {
169
                        @Override
170
                        public void onClick(ClickEvent event) {
171
                                updateFolder();
172
                                closeDialog();
173
                        }
174
                });
175
                ok.addStyleName("button");
176
                outer.add(ok);
177
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
178

    
179
        setWidget(outer);
180
        }
181

    
182
        @Override
183
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
184
                super.onPreviewNativeEvent(preview);
185

    
186
                NativeEvent evt = preview.getNativeEvent();
187
                if (evt.getType().equals(KeyDownEvent.getType().getName()))
188
                        // Use the popup's key preview hooks to close the dialog when either
189
                        // enter or escape is pressed.
190
                        switch (evt.getKeyCode()) {
191
                                case KeyCodes.KEY_ENTER:
192
                                        updateFolder();
193
                    closeDialog();
194
                                        break;
195
                                case KeyCodes.KEY_ESCAPE:
196
                                        closeDialog();
197
                                        break;
198
                        }
199
        }
200

    
201

    
202
        /**
203
         * Enables IE selection prevention and hides the dialog
204
         * (we disable the prevention on creation of the dialog)
205
         */
206
        public void closeDialog() {
207
                Pithos.preventIESelection();
208
                hide();
209
        }
210

    
211
        void updateFolder() {
212
        final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
213
        updateMetadata(folder.getUri() + "?update=", perms);
214
        }
215

    
216
        protected void updateMetadata(final String path, final Map<String, Boolean[]> newPermissions) {
217
        if (newPermissions != null) {
218
            PostRequest updateFolder = new PostRequest(app.getApiPath(), folder.getOwnerID(), path) {
219
                @Override
220
                public void onSuccess(Resource result) {
221
                    app.updateFolder(folder.getParent(), false, new Command() {
222
                                                
223
                                                @Override
224
                                                public void execute() {
225
                                                        app.updateMySharedRoot();
226
                                                }
227
                                        }, true);
228
                }
229

    
230
                @Override
231
                public void onError(Throwable t) {
232
                    GWT.log("", t);
233
                                        app.setError(t);
234
                    if (t instanceof RestException) {
235
                            if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) { //Probably a virtual folder
236
                            final String path1 = folder.getUri();
237
                            PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwnerID(), path1) {
238
                                @Override
239
                                public void onSuccess(Resource result) {
240
                                        updateMetadata(path, newPermissions);
241
                                }
242

    
243
                                @Override
244
                                public void onError(Throwable _t) {
245
                                    GWT.log("", _t);
246
                                                            app.setError(_t);
247
                                    if(_t instanceof RestException){
248
                                        app.displayError("Unable to update folder: " + ((RestException) _t).getHttpStatusText());
249
                                    }
250
                                    else
251
                                        app.displayError("System error modifying folder: " + _t.getMessage());
252
                                }
253

    
254
                                                @Override
255
                                                protected void onUnauthorized(Response response) {
256
                                                        app.sessionExpired();
257
                                                }
258
                            };
259
                            newFolder.setHeader("X-Auth-Token", app.getUserToken());
260
                            newFolder.setHeader("Content-Type", "application/folder");
261
                            newFolder.setHeader("Accept", "*/*");
262
                            newFolder.setHeader("Content-Length", "0");
263
                            Scheduler.get().scheduleDeferred(newFolder);
264
                            }
265
                            else if (((RestException) t).getHttpStatusCode() == Response.SC_CONFLICT) {
266
                                    app.displayError("Cannot set permissions. Probably subfolders or files already have permissions set");
267
                            }
268
                            else
269
                                    app.displayError("Εrror modifying folder: " + t.getMessage());
270
                    }
271
                    else
272
                            app.displayError("System error modifying folder: " + t.getMessage());
273
                }
274

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