Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FilePublishDialog.java @ 5d18aa82

History | View | Annotate | Download (10.4 kB)

1
/*
2
 * Copyright 2011-2012 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.Resource;
39
import gr.grnet.pithos.web.client.rest.HeadRequest;
40
import gr.grnet.pithos.web.client.rest.PostRequest;
41

    
42
import java.util.Map;
43

    
44
import com.google.gwt.core.client.GWT;
45
import com.google.gwt.core.client.Scheduler;
46
import com.google.gwt.dom.client.NativeEvent;
47
import com.google.gwt.event.dom.client.ClickEvent;
48
import com.google.gwt.event.dom.client.ClickHandler;
49
import com.google.gwt.event.dom.client.KeyCodes;
50
import com.google.gwt.http.client.Response;
51
import com.google.gwt.http.client.URL;
52
import com.google.gwt.http.client.UrlBuilder;
53
import com.google.gwt.i18n.client.Dictionary;
54
import com.google.gwt.resources.client.ImageResource;
55
import com.google.gwt.user.client.Command;
56
import com.google.gwt.user.client.Window;
57
import com.google.gwt.user.client.Event.NativePreviewEvent;
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.FocusPanel;
62
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
63
import com.google.gwt.user.client.ui.HorizontalPanel;
64
import com.google.gwt.user.client.ui.Label;
65
import com.google.gwt.user.client.ui.TextBox;
66
import com.google.gwt.user.client.ui.VerticalPanel;
67

    
68
/**
69
 * The 'File properties' dialog box implementation.
70
 *
71
 */
72
public class FilePublishDialog extends AbstractPropertiesDialog {
73

    
74
        protected CheckBox readForAll;
75
        
76
        private HorizontalPanel pathPanel;
77
        
78
        private TextBox path;
79
        
80
        /**
81
         * An image bundle for this widgets images.
82
         */
83
        public interface Images extends MessagePanel.Images {
84

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

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

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

    
95
        final File file;
96

    
97
    Images images = GWT.create(Images.class);
98

    
99
        /**
100
         * The widget's constructor.
101
         */
102
        public FilePublishDialog(Pithos _app, File _file) {
103
        super(_app);
104
        file = _file;
105

    
106
                Anchor close = new Anchor();
107
                close.addStyleName("close");
108
                close.addClickHandler(new ClickHandler() {
109
                        
110
                        @Override
111
                        public void onClick(ClickEvent event) {
112
                                hide();
113
                        }
114
                });
115
                // Set the dialog's caption.
116
                setText("Publish/Un-publish");
117
                setAnimationEnabled(true);
118
                setGlassEnabled(true);
119
                setStyleName("pithos-DialogBox");
120

    
121
                // Outer contains inner and buttons.
122
                final VerticalPanel outer = new VerticalPanel();
123
                outer.add(close);
124
                final FocusPanel focusPanel = new FocusPanel(outer);
125
                // Inner contains generalPanel and permPanel.
126
                inner = new VerticalPanel();
127
                inner.addStyleName("inner");
128

    
129
        inner.add(createSharingPanel());
130

    
131
        outer.add(inner);
132

    
133
                final Button ok = new Button("Close", new ClickHandler() {
134
                        @Override
135
                        public void onClick(ClickEvent event) {
136
                                closeDialog();
137
                        }
138
                });
139
                ok.addStyleName("button");
140

    
141
        outer.add(ok);
142
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
143

    
144
        focusPanel.setFocus(true);
145
        setWidget(outer);
146
        }
147

    
148
    private VerticalPanel createSharingPanel() {
149
        VerticalPanel permPanel = new VerticalPanel();
150

    
151
        final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
152
                    " by everyone. By checking this option, you are certifying that you have the right to " +
153
                    "distribute this file and that it does not violate the Terms of Use.", true);
154
        readForAllNote.setStylePrimaryName("pithos-readForAllNote");
155

    
156
        readForAll = new CheckBox();
157
        readForAll.setValue(file.isPublished());
158
        readForAll.addClickHandler(new ClickHandler() {
159
            @Override
160
            public void onClick(ClickEvent event) {
161
                    accept();
162
            }
163
        });
164

    
165
        // Only show the read for all permission if the user is the owner.
166
        if (file.getOwner().equals(app.getUsername())) {
167
            final HorizontalPanel permForAll = new HorizontalPanel();
168
            permForAll.add(new Label("Public"));
169
            permForAll.add(readForAll);
170
            permForAll.setSpacing(8);
171
            permForAll.addStyleName("pithos-TabPanelBottom");
172
            permForAll.add(readForAllNote);
173
            permPanel.add(permForAll);
174
        }
175

    
176
        pathPanel = new HorizontalPanel();
177
        pathPanel.setVisible(false);
178
        pathPanel.setWidth("100%");
179
        pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
180
        pathPanel.add(new Label("Link"));
181
        pathPanel.setSpacing(8);
182
        pathPanel.addStyleName("pithos-TabPanelBottom");
183

    
184
        path = new TextBox();
185
        path.setWidth("100%");
186
        path.addClickHandler(new ClickHandler() {
187
            @Override
188
            public void onClick(ClickEvent event) {
189
                Pithos.enableIESelection();
190
                ((TextBox) event.getSource()).selectAll();
191
                Pithos.preventIESelection();
192
            }
193
        });
194
        path.setText(Window.Location.getHost() + file.getPublicUri());
195
        path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
196
        path.setWidth("100%");
197
        path.setReadOnly(true);
198
        pathPanel.add(path);
199
        permPanel.add(pathPanel);
200

    
201
        Scheduler.get().scheduleDeferred(new Command() {
202
                        
203
                        @Override
204
                        public void execute() {
205
                                showLinkIfPublished();
206
                        }
207
                });
208
        return permPanel;
209
    }
210

    
211
    void showLinkIfPublished() {
212
                if (file.isPublished()) {
213
                        UrlBuilder b = Window.Location.createUrlBuilder();
214
                        b.setPath(file.getPublicUri());
215
                        path.setText(b.buildString());
216
                pathPanel.setVisible(true);
217
                }
218
                else {
219
                        pathPanel.setVisible(false);
220
                }
221
    }
222
        /**
223
         * Accepts any change and updates the file
224
         * @return 
225
         *
226
         */
227
        @Override
228
        protected boolean accept() {
229
        Boolean published = null;
230
                if (readForAll.getValue() != file.isPublished())
231
                        if (file.getOwner().equals(app.getUsername()))
232
                published = readForAll.getValue();
233
        updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", published);
234
        return true;
235
        }
236

    
237
        protected void updateMetaData(String api, String owner, final String path, final Boolean published) {
238
        if (published != null) {
239
            PostRequest updateFile = new PostRequest(api, owner, path) {
240
                @Override
241
                public void onSuccess(Resource result) {
242
                        HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwner(), path, file) {
243

    
244
                                                @Override
245
                                                public void onSuccess(File _result) {
246
                                                        showLinkIfPublished();
247
                                                        if (!app.isMySharedSelected())
248
                                            app.updateFolder(file.getParent(), true, new Command() {
249
                                                                        
250
                                                                        @Override
251
                                                                        public void execute() {
252
                                                                                app.updateMySharedRoot();
253
                                                                        }
254
                                                                }, true);
255
                                                        else
256
                                                                app.updateSharedFolder(file.getParent(), true);
257
                                                }
258

    
259
                                                @Override
260
                                                public void onError(Throwable t) {
261
                                    GWT.log("", t);
262
                                                        app.setError(t);
263
                                    app.displayError("System error modifying file:" + t.getMessage());
264
                                                }
265

    
266
                                                @Override
267
                                                protected void onUnauthorized(Response response) {
268
                                                        app.sessionExpired();
269
                                                }
270
                                        };
271
                                        headFile.setHeader("X-Auth-Token", app.getToken());
272
                                        Scheduler.get().scheduleDeferred(headFile);
273
                }
274

    
275
                @Override
276
                public void onError(Throwable t) {
277
                    GWT.log("", t);
278
                                        app.setError(t);
279
                    app.displayError("System error modifying file:" + t.getMessage());
280
                }
281

    
282
                                @Override
283
                                protected void onUnauthorized(Response response) {
284
                                        app.sessionExpired();
285
                                }
286
            };
287
            updateFile.setHeader("X-Auth-Token", app.getToken());
288
            updateFile.setHeader("X-Object-Public", published.toString());
289
            Scheduler.get().scheduleDeferred(updateFile);
290
        }
291
        else if (!app.isMySharedSelected())
292
            app.updateFolder(file.getParent(), true, new Command() {
293
                                
294
                                @Override
295
                                public void execute() {
296
                                        if (file.isSharedOrPublished())
297
                                                app.updateMySharedRoot();
298
                                }
299
                        }, true);
300
        else
301
                app.updateSharedFolder(file.getParent(), true);
302
    }
303

    
304
        @Override
305
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
306
            super.onPreviewNativeEvent(preview);
307

    
308
            NativeEvent evt = preview.getNativeEvent();
309
            if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)
310
                                closeDialog();
311
        }
312
}