Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / FilePropertiesDialog.java @ 50326f36

History | View | Annotate | Download (19.6 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 gr.grnet.pithos.web.client.foldertree.File;
39
import gr.grnet.pithos.web.client.foldertree.Resource;
40
import gr.grnet.pithos.web.client.rest.PostCommand;
41
import gr.grnet.pithos.web.client.rest.PostRequest;
42
import gr.grnet.pithos.web.client.rest.PutRequest;
43
import gr.grnet.pithos.web.client.rest.RestException;
44
import gr.grnet.pithos.web.client.rest.resource.FileResource;
45
import gr.grnet.pithos.web.client.rest.resource.GroupResource;
46
import gr.grnet.pithos.web.client.rest.resource.PermissionHolder;
47

    
48
import java.util.Iterator;
49
import java.util.List;
50
import java.util.Set;
51

    
52
import com.google.gwt.core.client.GWT;
53
import com.google.gwt.event.dom.client.ChangeEvent;
54
import com.google.gwt.event.dom.client.ChangeHandler;
55
import com.google.gwt.event.dom.client.ClickEvent;
56
import com.google.gwt.event.dom.client.ClickHandler;
57
import com.google.gwt.i18n.client.DateTimeFormat;
58
import com.google.gwt.json.client.JSONArray;
59
import com.google.gwt.json.client.JSONBoolean;
60
import com.google.gwt.json.client.JSONObject;
61
import com.google.gwt.json.client.JSONString;
62
import com.google.gwt.resources.client.ClientBundle;
63
import com.google.gwt.resources.client.ImageResource;
64
import com.google.gwt.user.client.Command;
65
import com.google.gwt.user.client.DeferredCommand;
66
import com.google.gwt.user.client.ui.AbstractImagePrototype;
67
import com.google.gwt.user.client.ui.Button;
68
import com.google.gwt.user.client.ui.CheckBox;
69
import com.google.gwt.user.client.ui.DecoratedTabPanel;
70
import com.google.gwt.user.client.ui.DisclosurePanel;
71
import com.google.gwt.user.client.ui.FlexTable;
72
import com.google.gwt.user.client.ui.FlowPanel;
73
import com.google.gwt.user.client.ui.FocusPanel;
74
import com.google.gwt.user.client.ui.HTML;
75
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
76
import com.google.gwt.user.client.ui.HorizontalPanel;
77
import com.google.gwt.user.client.ui.Label;
78
import com.google.gwt.user.client.ui.TextBox;
79
import com.google.gwt.user.client.ui.VerticalPanel;
80

    
81
/**
82
 * The 'File properties' dialog box implementation.
83
 *
84
 */
85
public class FilePropertiesDialog extends AbstractPropertiesDialog {
86

    
87
        private PermissionsList permList;
88

    
89
        private CheckBox readForAll;
90

    
91
        /**
92
         * An image bundle for this widgets images.
93
         */
94
        public interface Images extends ClientBundle,MessagePanel.Images {
95

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

    
99
                @Source("gr/grnet/pithos/resources/groupevent.png")
100
                ImageResource permGroup();
101

    
102
                @Source("gr/grnet/pithos/resources/editdelete.png")
103
                ImageResource delete();
104

    
105
                @Source("gr/grnet/pithos/resources/db_update.png")
106
                ImageResource restore();
107

    
108
                @Source("gr/grnet/pithos/resources/folder_inbox.png")
109
                ImageResource download();
110
        }
111

    
112
        /**
113
         * The widget that holds the name of the file.
114
         */
115
        private TextBox name = new TextBox();
116

    
117
        private final CheckBox versioned = new CheckBox();
118

    
119
        final File file;
120

    
121
        private String userFullName;
122

    
123
    private GSS app;
124

    
125
        /**
126
         * The widget's constructor.
127
         */
128
        public FilePropertiesDialog(GSS _app, File _file) {
129
        app = _app;
130
        file = _file;
131

    
132
                // Set the dialog's caption.
133
                setText("File properties");
134

    
135

    
136
//                permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
137

    
138
                // Outer contains inner and buttons.
139
                final VerticalPanel outer = new VerticalPanel();
140
                final FocusPanel focusPanel = new FocusPanel(outer);
141
                // Inner contains generalPanel and permPanel.
142
                inner = new DecoratedTabPanel();
143
                inner.setAnimationEnabled(true);
144

    
145

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

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

    
150
        inner.add(createVersionPanel(), "Versions");
151

    
152
        inner.selectTab(0);
153

    
154
        outer.add(inner);
155

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

    
167
                buttons.add(ok);
168
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
169
                // Create the 'Cancel' button, along with a listener that hides the
170
                // dialog when the button is clicked.
171
                final Button cancel = new Button("Cancel", new ClickHandler() {
172
                        @Override
173
                        public void onClick(ClickEvent event) {
174
                                closeDialog();
175
                        }
176
                });
177
                buttons.add(cancel);
178
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
179
                buttons.setSpacing(8);
180
                buttons.addStyleName("pithos-TabPanelBottom");
181

    
182
        outer.add(buttons);
183
        outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
184
        outer.addStyleName("pithos-TabPanelBottom");
185

    
186
        focusPanel.setFocus(true);
187
        setWidget(outer);
188

    
189
                // Asynchronously retrieve the tags defined by this user.
190
                DeferredCommand.addCommand(new Command() {
191

    
192
                        @Override
193
                        public void execute() {
194
//                                updateTags();
195
                        }
196
                });
197
        }
198

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

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

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

    
220
                // Get the tags.
221
//                StringBuffer tagsBuffer = new StringBuffer();
222
//                Iterator i = file.getTags().iterator();
223
//                while (i.hasNext()) {
224
//                        String tag = (String) i.next();
225
//                        tagsBuffer.append(tag).append(", ");
226
//                }
227
//                if (tagsBuffer.length() > 1)
228
//                        tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
229
//                initialTagText = tagsBuffer.toString();
230
//                tags.setWidth("100%");
231
//                tags.getElement().setId("filePropertiesDialog.textBox.tags");
232
//                tags.setText(initialTagText);
233
//                generalTable.setWidget(4, 1, tags);
234

    
235
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
236
        generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
237
        generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
238
        generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
239
//        generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
240
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
241
        generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
242
        generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
243
        generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
244
//        generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
245
        generalTable.setCellSpacing(4);
246

    
247
        generalPanel.add(generalTable);
248

    
249
        DisclosurePanel allTags = new DisclosurePanel("All tags");
250
        allTagsContent = new FlowPanel();
251
        allTagsContent.setWidth("100%");
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.isReadForAll());
297
//        readForAll.addClickHandler(new ClickHandler() {
298
//            @Override
299
//            public void onClick(ClickEvent event) {
300
//                readForAllNote.setVisible(readForAll.getValue());
301
//            }
302
//
303
//        });
304
//
305
//        // Only show the read for all permission if the user is the owner.
306
//        if (file.getOwner().equals(app.getUsername())) {
307
//            final HorizontalPanel permForAll = new HorizontalPanel();
308
//            permForAll.add(new Label("Public"));
309
//            permForAll.add(readForAll);
310
//            permForAll.setSpacing(8);
311
//            permForAll.addStyleName("pithos-TabPanelBottom");
312
//            permForAll.add(readForAllNote);
313
//            permPanel.add(permForAll);
314
//        }
315
//
316
//
317
//        final HorizontalPanel pathPanel = new HorizontalPanel();
318
//        pathPanel.setWidth("100%");
319
//        pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
320
//        pathPanel.add(new Label("Link"));
321
//        pathPanel.setSpacing(8);
322
//        pathPanel.addStyleName("pithos-TabPanelBottom");
323
//
324
//        TextBox path = new TextBox();
325
//        path.setWidth("100%");
326
//        path.addClickHandler(new ClickHandler() {
327
//            @Override
328
//            public void onClick(ClickEvent event) {
329
//                GSS.enableIESelection();
330
//                ((TextBox) event.getSource()).selectAll();
331
//                GSS.preventIESelection();
332
//            }
333
//        });
334
//        path.setText(file.getUri());
335
//        path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
336
//        path.setWidth("100%");
337
//        path.setReadOnly(true);
338
//        pathPanel.add(path);
339
//        permPanel.add(pathPanel);
340

    
341
        return permPanel;
342
    }
343

    
344
    private VerticalPanel createVersionPanel() {
345
        VerticalPanel versionPanel = new VerticalPanel();
346

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

    
394
        return versionPanel;
395
    }
396

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

    
454
        if (newFilename == null)
455
            return;
456
        String path = app.getApiPath() + app.getUsername() + file.getParent().getUri() + "/" + newFilename;
457
        PutRequest updateFile = new PutRequest(path) {
458
            @Override
459
            public void onSuccess(Resource result) {
460
                app.updateFolder(file.getParent());
461
            }
462

    
463
            @Override
464
            public void onError(Throwable t) {
465
                GWT.log("", t);
466
                app.displayError("System error modifying file:" + t.getMessage());
467
            }
468
        };
469
        updateFile.setHeader("X-Auth-Token", app.getToken());
470
        updateFile.setHeader("X-Move-From", file.getUri());
471
        updateFile.setHeader("Content-Type", file.getContentType());
472
        Scheduler.get().scheduleDeferred(updateFile);
473
        }
474

    
475
        private void removeAllOldVersions() {
476
                JSONObject json = new JSONObject();
477
                json.put("versioned", JSONBoolean.getInstance(false));
478
                GWT.log(json.toString(), null);
479
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
480

    
481
                        @Override
482
                        public void onComplete() {
483
                                toggleVersioned(true);
484
                        }
485

    
486
                        @Override
487
                        public void onError(Throwable t) {
488
                                GWT.log("", t);
489
                                if (t instanceof RestException) {
490
                                        int statusCode = ((RestException) t).getHttpStatusCode();
491
                                        if (statusCode == 405)
492
                                                GSS.get().displayError("You don't have the necessary permissions");
493
                                        else if (statusCode == 404)
494
                                                GSS.get().displayError("User in permissions does not exist");
495
                                        else if (statusCode == 409)
496
                                                GSS.get().displayError("A folder with the same name already exists");
497
                                        else if (statusCode == 413)
498
                                                GSS.get().displayError("Your quota has been exceeded");
499
                                        else
500
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
501
                                } else
502
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
503
                        }
504
                };
505
                DeferredCommand.addCommand(cf);
506
        }
507

    
508
        private void toggleVersioned(boolean versionedValue) {
509
                JSONObject json = new JSONObject();
510
                json.put("versioned", JSONBoolean.getInstance(versionedValue));
511
                GWT.log(json.toString(), null);
512
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
513

    
514
                        @Override
515
                        public void onComplete() {
516
                                GSS.get().getTreeView().refreshCurrentNode(false);
517
                        }
518

    
519
                        @Override
520
                        public void onError(Throwable t) {
521
                                GWT.log("", t);
522
                                if (t instanceof RestException) {
523
                                        int statusCode = ((RestException) t).getHttpStatusCode();
524
                                        if (statusCode == 405)
525
                                                GSS.get().displayError("You don't have the necessary permissions");
526
                                        else if (statusCode == 404)
527
                                                GSS.get().displayError("User in permissions does not exist");
528
                                        else if (statusCode == 409)
529
                                                GSS.get().displayError("A folder with the same name already exists");
530
                                        else if (statusCode == 413)
531
                                                GSS.get().displayError("Your quota has been exceeded");
532
                                        else
533
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
534
                                } else
535
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
536
                        }
537
                };
538
                DeferredCommand.addCommand(cf);
539
        }
540

    
541
}