Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (21.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 com.google.gwt.core.client.Scheduler;
38
import com.google.gwt.json.client.JSONArray;
39
import com.google.gwt.json.client.JSONString;
40
import com.google.gwt.user.client.ui.Anchor;
41
import com.google.gwt.user.client.ui.Label;
42
import gr.grnet.pithos.web.client.foldertree.File;
43
import gr.grnet.pithos.web.client.foldertree.Resource;
44
import gr.grnet.pithos.web.client.rest.PostCommand;
45
import gr.grnet.pithos.web.client.rest.PostRequest;
46
import gr.grnet.pithos.web.client.rest.PutRequest;
47
import gr.grnet.pithos.web.client.rest.RestException;
48

    
49
import com.google.gwt.core.client.GWT;
50
import com.google.gwt.event.dom.client.ClickEvent;
51
import com.google.gwt.event.dom.client.ClickHandler;
52
import com.google.gwt.i18n.client.DateTimeFormat;
53
import com.google.gwt.json.client.JSONBoolean;
54
import com.google.gwt.json.client.JSONObject;
55
import com.google.gwt.resources.client.ClientBundle;
56
import com.google.gwt.resources.client.ImageResource;
57
import com.google.gwt.user.client.Command;
58
import com.google.gwt.user.client.DeferredCommand;
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.TextBox;
69
import com.google.gwt.user.client.ui.VerticalPanel;
70
import gr.grnet.pithos.web.client.tagtree.Tag;
71
import java.util.Iterator;
72

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

    
79
        private PermissionsList permList;
80

    
81
        private CheckBox readForAll;
82

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

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

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

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

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

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

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

    
109
        private final CheckBox versioned = new CheckBox();
110

    
111
        final File file;
112

    
113
        private String userFullName;
114

    
115
    private Pithos app;
116

    
117
        /**
118
         * The widget's constructor.
119
         */
120
        public FilePropertiesDialog(Pithos _app, File _file) {
121
        app = _app;
122
        file = _file;
123

    
124
                // Set the dialog's caption.
125
                setText("File properties");
126

    
127

    
128
//                permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
129

    
130
                // Outer contains inner and buttons.
131
                final VerticalPanel outer = new VerticalPanel();
132
                final FocusPanel focusPanel = new FocusPanel(outer);
133
                // Inner contains generalPanel and permPanel.
134
                inner = new DecoratedTabPanel();
135
                inner.setAnimationEnabled(true);
136

    
137

    
138
        inner.add(createGeneralPanel(), "General");
139

    
140
        inner.add(createSharingPanel(), "Sharing");
141

    
142
        inner.add(createVersionPanel(), "Versions");
143

    
144
        inner.selectTab(0);
145

    
146
        outer.add(inner);
147

    
148
                final HorizontalPanel buttons = new HorizontalPanel();
149
                // Create the 'OK' button, along with a listener that hides the dialog
150
                // when the button is clicked.
151
                final Button ok = new Button("OK", new ClickHandler() {
152
                        @Override
153
                        public void onClick(ClickEvent event) {
154
                                accept();
155
                                closeDialog();
156
                        }
157
                });
158

    
159
                buttons.add(ok);
160
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
161
                // Create the 'Cancel' button, along with a listener that hides the
162
                // dialog when the button is clicked.
163
                final Button cancel = new Button("Cancel", new ClickHandler() {
164
                        @Override
165
                        public void onClick(ClickEvent event) {
166
                                closeDialog();
167
                        }
168
                });
169
                buttons.add(cancel);
170
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
171
                buttons.setSpacing(8);
172
                buttons.addStyleName("pithos-TabPanelBottom");
173

    
174
        outer.add(buttons);
175
        outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
176
        outer.addStyleName("pithos-TabPanelBottom");
177

    
178
        focusPanel.setFocus(true);
179
        setWidget(outer);
180
        }
181

    
182
    private VerticalPanel createGeneralPanel() {
183
        final VerticalPanel generalPanel = new VerticalPanel();
184
        final FlexTable generalTable = new FlexTable();
185
        generalTable.setText(0, 0, "Name");
186
        generalTable.setText(1, 0, "Folder");
187
        generalTable.setText(2, 0, "Owner");
188
        generalTable.setText(3, 0, "Last modified");
189
        generalTable.setText(4, 0, "Tags");
190

    
191
        name.setWidth("100%");
192
        name.setText(file.getName());
193
        generalTable.setWidget(0, 1, name);
194
        if(file.getParent() != null)
195
            generalTable.setText(1, 1, file.getParent().getName());
196
        else
197
            generalTable.setText(1, 1, "-");
198
        generalTable.setText(2, 1, file.getOwner());
199

    
200
        final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
201
        generalTable.setText(3, 1, formatter.format(file.getLastModified()));
202

    
203
                StringBuffer tagsBuffer = new StringBuffer();
204
        for (String t : file.getTags())
205
                        tagsBuffer.append(t).append(", ");
206
                if (tagsBuffer.length() > 1)
207
                        tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
208
                initialTagText = tagsBuffer.toString();
209
                tags.setWidth("100%");
210
                tags.setText(initialTagText);
211
                generalTable.setWidget(4, 1, tags);
212

    
213
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
214
        generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
215
        generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
216
        generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
217
        generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
218
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
219
        generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
220
        generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
221
        generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
222
        generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
223
        generalTable.setCellSpacing(4);
224

    
225
        generalPanel.add(generalTable);
226

    
227
        DisclosurePanel allTags = new DisclosurePanel("All tags");
228
        allTagsContent = new FlowPanel();
229
        allTagsContent.setWidth("100%");
230
        for (Tag t : app.getAllTags()) {
231
            final Anchor tagAnchor = new Anchor(t.getName(), false);
232
            tagAnchor.addStyleName("pithos-tag");
233
            allTagsContent.add(tagAnchor);
234
            Label separator = new Label(", ");
235
            separator.addStyleName("pithos-tag");
236
            allTagsContent.add(separator);
237
            tagAnchor.addClickHandler(new ClickHandler() {
238

    
239
                @Override
240
                public void onClick(ClickEvent event) {
241
                    String existing = tags.getText().trim();
242
                    if (MULTIPLE_VALUES_TEXT.equals(existing))
243
                        existing = "";
244
                    String newTag = tagAnchor.getText().trim();
245
                    // insert the new tag only if it is not in the list
246
                    // already
247
                    if (existing.indexOf(newTag) == -1)
248
                        tags.setText(existing + (existing.length() > 0 ? ", " : "") + newTag);
249
                }
250
            });
251
        }
252
        allTags.setContent(allTagsContent);
253
        generalPanel.add(allTags);
254
        generalPanel.setSpacing(4);
255
        return generalPanel;
256
    }
257

    
258
    private VerticalPanel createSharingPanel() {
259
        VerticalPanel permPanel = new VerticalPanel();
260
//
261
//        permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
262
//        permPanel.add(permList);
263
//
264
//        HorizontalPanel permButtons = new HorizontalPanel();
265
//        Button add = new Button("Add Group", new ClickHandler() {
266
//            @Override
267
//            public void onClick(ClickEvent event) {
268
//                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
269
//                dlg.center();
270
//            }
271
//        });
272
//        permButtons.add(add);
273
//        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
274
//
275
//        final Button addUser = new Button("Add User", new ClickHandler() {
276
//            @Override
277
//            public void onClick(ClickEvent event) {
278
//                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
279
//                dlg.center();
280
//            }
281
//        });
282
//        permButtons.add(addUser);
283
//        permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
284
//
285
//        permButtons.setSpacing(8);
286
//        permButtons.addStyleName("pithos-TabPanelBottom");
287
//        permPanel.add(permButtons);
288
//
289
        final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
290
                    " by everyone. By checking this option, you are certifying that you have the right to " +
291
                    "distribute this file and that it does not violate the Terms of Use.", true);
292
        readForAllNote.setVisible(false);
293
        readForAllNote.setStylePrimaryName("pithos-readForAllNote");
294

    
295
        readForAll = new CheckBox();
296
        readForAll.setValue(file.isPublished());
297
        readForAll.addClickHandler(new ClickHandler() {
298
            @Override
299
            public void onClick(ClickEvent event) {
300
                readForAllNote.setVisible(readForAll.getValue());
301
            }
302
        });
303

    
304
        // Only show the read for all permission if the user is the owner.
305
        if (file.getOwner().equals(app.getUsername())) {
306
            final HorizontalPanel permForAll = new HorizontalPanel();
307
            permForAll.add(new Label("Public"));
308
            permForAll.add(readForAll);
309
            permForAll.setSpacing(8);
310
            permForAll.addStyleName("pithos-TabPanelBottom");
311
            permForAll.add(readForAllNote);
312
            permPanel.add(permForAll);
313
        }
314

    
315
        final HorizontalPanel pathPanel = new HorizontalPanel();
316
        pathPanel.setWidth("100%");
317
        pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
318
        pathPanel.add(new Label("Link"));
319
        pathPanel.setSpacing(8);
320
        pathPanel.addStyleName("pithos-TabPanelBottom");
321

    
322
        TextBox path = new TextBox();
323
        path.setWidth("100%");
324
        path.addClickHandler(new ClickHandler() {
325
            @Override
326
            public void onClick(ClickEvent event) {
327
                Pithos.enableIESelection();
328
                ((TextBox) event.getSource()).selectAll();
329
                Pithos.preventIESelection();
330
            }
331
        });
332
        path.setText(file.getUri());
333
        path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
334
        path.setWidth("100%");
335
        path.setReadOnly(true);
336
        pathPanel.add(path);
337
        permPanel.add(pathPanel);
338

    
339
        return permPanel;
340
    }
341

    
342
    private VerticalPanel createVersionPanel() {
343
        VerticalPanel versionPanel = new VerticalPanel();
344

    
345
//        VersionsList verList = new VersionsList(this, images, bodies);
346
//        versionPanel.add(verList);
347
//
348
//        HorizontalPanel vPanel = new HorizontalPanel();
349
//
350
//                vPanel.setSpacing(8);
351
//                vPanel.addStyleName("pithos-TabPanelBottom");
352
//                vPanel.add(new Label("Versioned"));
353
//
354
//                versioned.setValue(file.isVersioned());
355
//                vPanel.add(versioned);
356
//                versionPanel.add(vPanel);
357
//
358
//        HorizontalPanel vPanel2 = new HorizontalPanel();
359
//                vPanel2.setSpacing(8);
360
//                vPanel2.addStyleName("pithos-TabPanelBottom");
361
//
362
//        HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
363
//        vPanel2.add(removeAllVersion);
364
//
365
//                Button removeVersionsButton = new Button(AbstractImagePrototype.create(images.delete()).getHTML(), new ClickHandler() {
366
//                        @Override
367
//                        public void onClick(ClickEvent event) {
368
//                                ConfirmationDialog confirm = new ConfirmationDialog("Really " +
369
//                                                "remove all previous versions?", "Remove") {
370
//
371
//                                        @Override
372
//                                        public void cancel() {
373
//                                        }
374
//
375
//                                        @Override
376
//                                        public void confirm() {
377
//                                                FilePropertiesDialog.this.closeDialog();
378
//                                                removeAllOldVersions();
379
//                                        }
380
//
381
//                                };
382
//                                confirm.center();
383
//                        }
384
//
385
//                });
386
//                vPanel2.add(removeVersionsButton);
387
//        if(!file.isVersioned())
388
//            vPanel2.setVisible(false);
389
//
390
//        versionPanel.add(vPanel2);
391

    
392
        return versionPanel;
393
    }
394

    
395
        /**
396
         * Accepts any change and updates the file
397
         *
398
         */
399
        @Override
400
        protected void accept() {
401
                String newFilename = null;
402
//                permList.updatePermissionsAccordingToInput();
403
//                Set<PermissionHolder> perms = permList.getPermissions();
404
//                JSONObject json = new JSONObject();
405
                if (!name.getText().trim().equals(file.getName())) {
406
                        newFilename = name.getText().trim();
407
//                        json.put("name", new JSONString(newFilename));
408
                }
409
//                if (versioned.getValue() != file.isVersioned())
410
//                        json.put("versioned", JSONBoolean.getInstance(versioned.getValue()));
411
                //only update the read for all perm if the user is the owner
412
        Boolean published = null;
413
                if (readForAll.getValue() != file.isPublished())
414
                        if (file.getOwner().equals(Pithos.get().getUsername()))
415
                published = readForAll.getValue();
416
        final Boolean finalPublished = published;
417
//                                json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
418
//                int i = 0;
419
//                if (permList.hasChanges()) {
420
//                        GWT.log("Permissions change", null);
421
//                        JSONArray perma = new JSONArray();
422
//
423
//                        for (PermissionHolder p : perms) {
424
//                                JSONObject po = new JSONObject();
425
//                                if (p.getUser() != null)
426
//                                        po.put("user", new JSONString(p.getUser()));
427
//                                if (p.getGroup() != null)
428
//                                        po.put("group", new JSONString(p.getGroup()));
429
//                                po.put("read", JSONBoolean.getInstance(p.isRead()));
430
//                                po.put("write", JSONBoolean.getInstance(p.isWrite()));
431
//                                po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
432
//                                perma.set(i, po);
433
//                                i++;
434
//                        }
435
//                        json.put("permissions", perma);
436
//                }
437
        String[] tagset = null;
438
                if (!tags.getText().equals(initialTagText))
439
                        tagset = tags.getText().split(",");
440
//                String jsonString = json.toString();
441
//                if(jsonString.equals("{}")){
442
//                        GWT.log("NO CHANGES", null);
443
//                        return;
444
//                }
445
        final String[] newTags = tagset;
446

    
447
        if (newFilename != null) {
448
            final String path = file.getParent().getUri() + "/" + newFilename;
449
            PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
450
                @Override
451
                public void onSuccess(Resource result) {
452
                    if (newTags != null || finalPublished != null)
453
                        updateMetaData(app.getApiPath(), app.getUsername(), path + "?update=", newTags, finalPublished);
454
                    else
455
                        app.updateFolder(file.getParent());
456
                }
457

    
458
                @Override
459
                public void onError(Throwable t) {
460
                    GWT.log("", t);
461
                    app.displayError("System error modifying file:" + t.getMessage());
462
                }
463
            };
464
            updateFile.setHeader("X-Auth-Token", app.getToken());
465
            updateFile.setHeader("X-Move-From", file.getUri());
466
            updateFile.setHeader("Content-Type", file.getContentType());
467
            Scheduler.get().scheduleDeferred(updateFile);
468
        }
469
        else if (newTags != null || finalPublished != null)
470
            updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished);
471
        }
472

    
473
    private void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published) {
474
        PostRequest updateFile = new PostRequest(api, owner, path) {
475
            @Override
476
            public void onSuccess(Resource result) {
477
                app.updateFolder(file.getParent());
478
            }
479

    
480
            @Override
481
            public void onError(Throwable t) {
482
                GWT.log("", t);
483
                app.displayError("System error modifying file:" + t.getMessage());
484
            }
485
        };
486
        updateFile.setHeader("X-Auth-Token", app.getToken());
487
        if (newTags != null)
488
            for (String t : newTags)
489
                updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");
490
        if (published != null)
491
            updateFile.setHeader("X-Object-Public", published.toString());
492
        Scheduler.get().scheduleDeferred(updateFile);
493
    }
494

    
495
        private void removeAllOldVersions() {
496
                JSONObject json = new JSONObject();
497
                json.put("versioned", JSONBoolean.getInstance(false));
498
                GWT.log(json.toString(), null);
499
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
500

    
501
                        @Override
502
                        public void onComplete() {
503
                                toggleVersioned(true);
504
                        }
505

    
506
                        @Override
507
                        public void onError(Throwable t) {
508
                                GWT.log("", t);
509
                                if (t instanceof RestException) {
510
                                        int statusCode = ((RestException) t).getHttpStatusCode();
511
                                        if (statusCode == 405)
512
                                                Pithos.get().displayError("You don't have the necessary permissions");
513
                                        else if (statusCode == 404)
514
                                                Pithos.get().displayError("User in permissions does not exist");
515
                                        else if (statusCode == 409)
516
                                                Pithos.get().displayError("A folder with the same name already exists");
517
                                        else if (statusCode == 413)
518
                                                Pithos.get().displayError("Your quota has been exceeded");
519
                                        else
520
                                                Pithos.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
521
                                } else
522
                                        Pithos.get().displayError("System error moifying file:" + t.getMessage());
523
                        }
524
                };
525
                DeferredCommand.addCommand(cf);
526
        }
527

    
528
        private void toggleVersioned(boolean versionedValue) {
529
                JSONObject json = new JSONObject();
530
                json.put("versioned", JSONBoolean.getInstance(versionedValue));
531
                GWT.log(json.toString(), null);
532
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
533

    
534
                        @Override
535
                        public void onComplete() {
536
                                Pithos.get().getTreeView().refreshCurrentNode(false);
537
                        }
538

    
539
                        @Override
540
                        public void onError(Throwable t) {
541
                                GWT.log("", t);
542
                                if (t instanceof RestException) {
543
                                        int statusCode = ((RestException) t).getHttpStatusCode();
544
                                        if (statusCode == 405)
545
                                                Pithos.get().displayError("You don't have the necessary permissions");
546
                                        else if (statusCode == 404)
547
                                                Pithos.get().displayError("User in permissions does not exist");
548
                                        else if (statusCode == 409)
549
                                                Pithos.get().displayError("A folder with the same name already exists");
550
                                        else if (statusCode == 413)
551
                                                Pithos.get().displayError("Your quota has been exceeded");
552
                                        else
553
                                                Pithos.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
554
                                } else
555
                                        Pithos.get().displayError("System error moifying file:" + t.getMessage());
556
                        }
557
                };
558
                DeferredCommand.addCommand(cf);
559
        }
560

    
561
}