Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (19.4 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.http.client.URL;
56
import com.google.gwt.i18n.client.DateTimeFormat;
57
import com.google.gwt.resources.client.ImageResource;
58
import com.google.gwt.user.client.Window;
59
import com.google.gwt.user.client.ui.Anchor;
60
import com.google.gwt.user.client.ui.Button;
61
import com.google.gwt.user.client.ui.CheckBox;
62
import com.google.gwt.user.client.ui.DecoratedTabPanel;
63
import com.google.gwt.user.client.ui.DisclosurePanel;
64
import com.google.gwt.user.client.ui.FlexTable;
65
import com.google.gwt.user.client.ui.FlowPanel;
66
import com.google.gwt.user.client.ui.FocusPanel;
67
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
68
import com.google.gwt.user.client.ui.HorizontalPanel;
69
import com.google.gwt.user.client.ui.Label;
70
import com.google.gwt.user.client.ui.TextBox;
71
import com.google.gwt.user.client.ui.VerticalPanel;
72

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

    
79
        protected PermissionsList permList;
80

    
81
        protected CheckBox readForAll;
82

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

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

    
91
                @Source("gr/grnet/pithos/resources/groups22.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
        final File file;
110

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

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

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

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

    
145

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

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

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

    
154
        outer.add(inner);
155

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

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

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

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

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

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

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

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

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

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

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

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

    
245
        generalPanel.add(generalTable);
246

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

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

    
278
    private VerticalPanel createSharingPanel() {
279
        VerticalPanel permPanel = new VerticalPanel();
280

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

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

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

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

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

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

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

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

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

    
367
        return permPanel;
368
    }
369

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

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

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

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

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

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

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

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

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

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

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

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