Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FilePropertiesDialog.java @ 969a4d94

History | View | Annotate | Download (19.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.FileVersions;
39
import gr.grnet.pithos.web.client.foldertree.Resource;
40
import gr.grnet.pithos.web.client.foldertree.Version;
41
import gr.grnet.pithos.web.client.rest.GetRequest;
42
import gr.grnet.pithos.web.client.rest.PostRequest;
43
import gr.grnet.pithos.web.client.rest.PutRequest;
44
import gr.grnet.pithos.web.client.rest.RestException;
45
import gr.grnet.pithos.web.client.tagtree.Tag;
46

    
47
import java.util.List;
48
import java.util.Map;
49

    
50
import com.google.gwt.core.client.GWT;
51
import com.google.gwt.core.client.Scheduler;
52
import com.google.gwt.event.dom.client.ClickEvent;
53
import com.google.gwt.event.dom.client.ClickHandler;
54
import com.google.gwt.http.client.Response;
55
import com.google.gwt.i18n.client.DateTimeFormat;
56
import com.google.gwt.resources.client.ImageResource;
57
import com.google.gwt.user.client.Window;
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.DecoratedTabPanel;
62
import com.google.gwt.user.client.ui.DisclosurePanel;
63
import com.google.gwt.user.client.ui.FlexTable;
64
import com.google.gwt.user.client.ui.FlowPanel;
65
import com.google.gwt.user.client.ui.FocusPanel;
66
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
67
import com.google.gwt.user.client.ui.HorizontalPanel;
68
import com.google.gwt.user.client.ui.Label;
69
import com.google.gwt.user.client.ui.TextBox;
70
import com.google.gwt.user.client.ui.VerticalPanel;
71

    
72
/**
73
 * The 'File properties' dialog box implementation.
74
 *
75
 */
76
public class FilePropertiesDialog extends AbstractPropertiesDialog {
77

    
78
        protected PermissionsList permList;
79

    
80
        protected CheckBox readForAll;
81

    
82
        /**
83
         * An image bundle for this widgets images.
84
         */
85
        public interface Images extends MessagePanel.Images {
86

    
87
                @Source("gr/grnet/pithos/resources/edit_user.png")
88
                ImageResource permUser();
89

    
90
                @Source("gr/grnet/pithos/resources/groups22.png")
91
                ImageResource permGroup();
92

    
93
                @Source("gr/grnet/pithos/resources/editdelete.png")
94
                ImageResource delete();
95

    
96
                @Source("gr/grnet/pithos/resources/db_update.png")
97
                ImageResource restore();
98

    
99
                @Source("gr/grnet/pithos/resources/folder_inbox.png")
100
                ImageResource download();
101
        }
102

    
103
        /**
104
         * The widget that holds the name of the file.
105
         */
106
        private TextBox name = new TextBox();
107

    
108
        final File file;
109

    
110
    Images images = GWT.create(Images.class);
111

    
112
        /**
113
         * The widget's constructor.
114
         */
115
        public FilePropertiesDialog(Pithos _app, File _file) {
116
        super(_app);
117
        file = _file;
118

    
119
                Anchor close = new Anchor();
120
                close.addStyleName("close");
121
                close.addClickHandler(new ClickHandler() {
122
                        
123
                        @Override
124
                        public void onClick(ClickEvent event) {
125
                                hide();
126
                        }
127
                });
128
                // Set the dialog's caption.
129
                setText("File properties");
130
                setAnimationEnabled(true);
131
                setGlassEnabled(true);
132
                setStyleName("pithos-DialogBox");
133

    
134
                // Outer contains inner and buttons.
135
                final VerticalPanel outer = new VerticalPanel();
136
                outer.add(close);
137
                final FocusPanel focusPanel = new FocusPanel(outer);
138
                // Inner contains generalPanel and permPanel.
139
                inner = new DecoratedTabPanel();
140
                inner.setAnimationEnabled(true);
141
                inner.addStyleName("inner");
142
                inner.getDeckPanel().addStyleName("pithos-TabPanelBottom");
143

    
144

    
145
        inner.add(createGeneralPanel(), "General");
146

    
147
        inner.add(createSharingPanel(), "Sharing");
148

    
149
                fetchVersions();
150
                        
151
        inner.selectTab(0);
152

    
153
        outer.add(inner);
154

    
155
                // Create the 'OK' button, along with a listener that hides the dialog
156
                // when the button is clicked.
157
                final Button ok = new Button("OK", new ClickHandler() {
158
                        @Override
159
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
160
                                accept();
161
                                closeDialog();
162
                        }
163
                });
164
                ok.addStyleName("button");
165

    
166
        outer.add(ok);
167
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
168

    
169
        focusPanel.setFocus(true);
170
        setWidget(outer);
171
        }
172

    
173
    protected void fetchVersions() {
174
            String path = file.getUri() + "?format=json&version=list";
175
            GetRequest<FileVersions> getVersions = new GetRequest<FileVersions>(FileVersions.class, app.getApiPath(), file.getOwner(), path) {
176

    
177
                        @Override
178
                        public void onSuccess(FileVersions _result) {
179
                        inner.add(createVersionPanel(_result.getVersions()), "Versions");
180
                        }
181

    
182
                        @Override
183
                        public void onError(Throwable t) {
184
                if (t instanceof RestException) {
185
                    app.displayError("Unable to fetch versions: " + ((RestException) t).getHttpStatusText());
186
                }
187
                else
188
                    app.displayError("System error unable to fetch versions: "+t.getMessage());
189
                        }
190

    
191
                        @Override
192
                        protected void onUnauthorized(Response response) {
193
                                app.sessionExpired();
194
                        }
195
                };
196
                getVersions.setHeader("X-Auth-Token", app.getToken());
197
                Scheduler.get().scheduleDeferred(getVersions);
198
        }
199

    
200
        private VerticalPanel createGeneralPanel() {
201
        final VerticalPanel generalPanel = new VerticalPanel();
202
        final FlexTable generalTable = new FlexTable();
203
        generalTable.setText(0, 0, "Name");
204
        generalTable.setText(1, 0, "Folder");
205
        generalTable.setText(2, 0, "Owner");
206
        generalTable.setText(3, 0, "Last modified");
207
        generalTable.setText(4, 0, "Tags");
208

    
209
        name.setWidth("100%");
210
        name.setText(file.getName());
211
        generalTable.setWidget(0, 1, name);
212
        if(file.getParent() != null)
213
            generalTable.setText(1, 1, file.getParent().getName());
214
        else
215
            generalTable.setText(1, 1, "-");
216
        generalTable.setText(2, 1, file.getOwner());
217

    
218
        final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
219
        generalTable.setText(3, 1, file.getLastModified() != null ? formatter.format(file.getLastModified()) : "");
220

    
221
                StringBuffer tagsBuffer = new StringBuffer();
222
        for (String t : file.getTags())
223
                        tagsBuffer.append(t).append(", ");
224
                if (tagsBuffer.length() > 1)
225
                        tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
226
                initialTagText = tagsBuffer.toString();
227
                tags.setWidth("100%");
228
                tags.setText(initialTagText);
229
                generalTable.setWidget(4, 1, tags);
230

    
231
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
232
        generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
233
        generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
234
        generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
235
        generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
236
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
237
        generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
238
        generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
239
        generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
240
        generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
241
        generalTable.setCellSpacing(4);
242

    
243
        generalPanel.add(generalTable);
244

    
245
        DisclosurePanel allTags = new DisclosurePanel("All tags");
246
        allTagsContent = new FlowPanel();
247
        allTagsContent.setWidth("100%");
248
        for (Tag t : app.getAllTags()) {
249
            final Anchor tagAnchor = new Anchor(t.getName(), false);
250
            tagAnchor.addStyleName("pithos-tag");
251
            allTagsContent.add(tagAnchor);
252
            Label separator = new Label(", ");
253
            separator.addStyleName("pithos-tag");
254
            allTagsContent.add(separator);
255
            tagAnchor.addClickHandler(new ClickHandler() {
256

    
257
                @Override
258
                public void onClick(@SuppressWarnings("unused") ClickEvent event) {
259
                    String existing = tags.getText().trim();
260
                    if (MULTIPLE_VALUES_TEXT.equals(existing))
261
                        existing = "";
262
                    String newTag = tagAnchor.getText().trim();
263
                    // insert the new tag only if it is not in the list
264
                    // already
265
                    if (existing.indexOf(newTag) == -1)
266
                        tags.setText(existing + (existing.length() > 0 ? ", " : "") + newTag);
267
                }
268
            });
269
        }
270
        allTags.setContent(allTagsContent);
271
        generalPanel.add(allTags);
272
        generalPanel.setSpacing(4);
273
        return generalPanel;
274
    }
275

    
276
    private VerticalPanel createSharingPanel() {
277
        VerticalPanel permPanel = new VerticalPanel();
278

    
279
        permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);
280
        permPanel.add(permList);
281

    
282
        if (file.getInheritedPermissionsFrom() == null) {
283
            HorizontalPanel permButtons = new HorizontalPanel();
284
            Button add = new Button("Add Group", new ClickHandler() {
285
                @Override
286
                public void onClick(@SuppressWarnings("unused") ClickEvent event) {
287
                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
288
                    dlg.center();
289
                    permList.updatePermissionTable();
290
                }
291
            });
292
            add.addStyleName("button");
293
            permButtons.add(add);
294
            permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
295

    
296
            final Button addUser = new Button("Add User", new ClickHandler() {
297
                @Override
298
                public void onClick(@SuppressWarnings("unused") ClickEvent event) {
299
                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
300
                    dlg.center();
301
                    permList.updatePermissionTable();
302
                }
303
            });
304
            addUser.addStyleName("button");
305
            permButtons.add(addUser);
306
            permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
307

    
308
            permButtons.setSpacing(8);
309
            permButtons.addStyleName("pithos-TabPanelBottom");
310
            permPanel.add(permButtons);
311
        }
312

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

    
319
        readForAll = new CheckBox();
320
        readForAll.setValue(file.isPublished());
321
        readForAll.addClickHandler(new ClickHandler() {
322
            @Override
323
            public void onClick(@SuppressWarnings("unused") ClickEvent event) {
324
                readForAllNote.setVisible(readForAll.getValue());
325
            }
326
        });
327

    
328
        // Only show the read for all permission if the user is the owner.
329
        if (file.getOwner().equals(app.getUsername())) {
330
            final HorizontalPanel permForAll = new HorizontalPanel();
331
            permForAll.add(new Label("Public"));
332
            permForAll.add(readForAll);
333
            permForAll.setSpacing(8);
334
            permForAll.addStyleName("pithos-TabPanelBottom");
335
            permForAll.add(readForAllNote);
336
            permPanel.add(permForAll);
337
        }
338

    
339
        if (file.isPublished()) {
340
            final HorizontalPanel pathPanel = new HorizontalPanel();
341
            pathPanel.setWidth("100%");
342
            pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
343
            pathPanel.add(new Label("Link"));
344
            pathPanel.setSpacing(8);
345
            pathPanel.addStyleName("pithos-TabPanelBottom");
346

    
347
            TextBox path = new TextBox();
348
            path.setWidth("100%");
349
            path.addClickHandler(new ClickHandler() {
350
                @Override
351
                public void onClick(ClickEvent event) {
352
                    Pithos.enableIESelection();
353
                    ((TextBox) event.getSource()).selectAll();
354
                    Pithos.preventIESelection();
355
                }
356
            });
357
            path.setText(Window.Location.getHost() + file.getPublicUri());
358
            path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
359
            path.setWidth("100%");
360
            path.setReadOnly(true);
361
            pathPanel.add(path);
362
            permPanel.add(pathPanel);
363
        }
364

    
365
        return permPanel;
366
    }
367

    
368
    VerticalPanel createVersionPanel(List<Version> versions) {
369
        VerticalPanel versionPanel = new VerticalPanel();
370
        VersionsList verList = new VersionsList(app, this, images, file, versions);
371
        versionPanel.add(verList);
372
        return versionPanel;
373
    }
374

    
375
        /**
376
         * Accepts any change and updates the file
377
         *
378
         */
379
        @Override
380
        protected void accept() {
381
                String newFilename = null;
382

    
383
                final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
384

    
385
                if (!name.getText().trim().equals(file.getName())) {
386
                        newFilename = name.getText().trim();
387
                }
388

    
389
                //only update the read for all perm if the user is the owner
390
        Boolean published = null;
391
                if (readForAll.getValue() != file.isPublished())
392
                        if (file.getOwner().equals(app.getUsername()))
393
                published = readForAll.getValue();
394
        final Boolean finalPublished = published;
395

    
396
        String[] tagset = null;
397
                if (!tags.getText().equals(initialTagText))
398
                        tagset = tags.getText().trim().split(",");
399
        final String[] newTags = tagset;
400

    
401
        if (newFilename != null) {
402
            final String path = file.getParent().getUri() + "/" + newFilename;
403
            PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
404
                @Override
405
                public void onSuccess(@SuppressWarnings("unused") Resource result) {
406
                    updateMetaData(app.getApiPath(), file.getOwner(), path + "?update=", newTags, finalPublished, perms);
407
                }
408

    
409
                @Override
410
                public void onError(Throwable t) {
411
                    GWT.log("", t);
412
                    app.displayError("System error modifying file:" + t.getMessage());
413
                }
414

    
415
                                @Override
416
                                protected void onUnauthorized(Response response) {
417
                                        app.sessionExpired();
418
                                }
419
            };
420
            updateFile.setHeader("X-Auth-Token", app.getToken());
421
            updateFile.setHeader("X-Move-From", file.getUri());
422
            updateFile.setHeader("Content-Type", file.getContentType());
423
            Scheduler.get().scheduleDeferred(updateFile);
424
        }
425
        else
426
            updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished, perms);
427
        }
428

    
429
        protected void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published, Map<String, Boolean[]> newPermissions) {
430
        if (newTags != null || published != null || newPermissions != null) {
431
            PostRequest updateFile = new PostRequest(api, owner, path) {
432
                @Override
433
                public void onSuccess(@SuppressWarnings("unused") Resource result) {
434
                    app.updateFolder(file.getParent(), true, null);
435
                }
436

    
437
                @Override
438
                public void onError(Throwable t) {
439
                    GWT.log("", t);
440
                    app.displayError("System error modifying file:" + t.getMessage());
441
                }
442

    
443
                                @Override
444
                                protected void onUnauthorized(Response response) {
445
                                        app.sessionExpired();
446
                                }
447
            };
448
            updateFile.setHeader("X-Auth-Token", app.getToken());
449
            if (newTags != null)
450
                for (String t : newTags)
451
                        if (t.length() > 0)
452
                                updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");
453
                for (String t : file.getTags())
454
                            updateFile.setHeader("X-Object-Meta-" + t.trim(), "~");
455
            if (published != null)
456
                updateFile.setHeader("X-Object-Public", published.toString());
457
            if (newPermissions != null) {
458
                String readPermHeader = "read=";
459
                String writePermHeader = "write=";
460
                for (String u : newPermissions.keySet()) {
461
                    Boolean[] p = newPermissions.get(u);
462
                    if (p[0] != null && p[0])
463
                        readPermHeader += u + ",";
464
                    if (p[1] != null && p[1])
465
                        writePermHeader += u + ",";
466
                }
467
                if (readPermHeader.endsWith("="))
468
                    readPermHeader = "";
469
                else if (readPermHeader.endsWith(","))
470
                    readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
471
                if (writePermHeader.endsWith("="))
472
                    writePermHeader = "";
473
                else if (writePermHeader.endsWith(","))
474
                    writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
475
                String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
476
                if (permHeader.length() == 0)
477
                    permHeader="~";
478
                updateFile.setHeader("X-Object-Sharing", permHeader);
479
            }
480
            Scheduler.get().scheduleDeferred(updateFile);
481
        }
482
        else
483
            app.updateFolder(file.getParent(), true, null);
484
    }
485
}