Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.3 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.rest.HeadRequest;
39
import gr.grnet.pithos.web.client.rest.PostRequest;
40

    
41
import com.google.gwt.core.client.GWT;
42
import com.google.gwt.core.client.Scheduler;
43
import com.google.gwt.dom.client.NativeEvent;
44
import com.google.gwt.event.dom.client.ClickEvent;
45
import com.google.gwt.event.dom.client.ClickHandler;
46
import com.google.gwt.event.dom.client.KeyCodes;
47
import com.google.gwt.http.client.Response;
48
import com.google.gwt.http.client.UrlBuilder;
49
import com.google.gwt.resources.client.ImageResource;
50
import com.google.gwt.user.client.Command;
51
import com.google.gwt.user.client.Window;
52
import com.google.gwt.user.client.Event.NativePreviewEvent;
53
import com.google.gwt.user.client.ui.Anchor;
54
import com.google.gwt.user.client.ui.Button;
55
import com.google.gwt.user.client.ui.CheckBox;
56
import com.google.gwt.user.client.ui.FocusPanel;
57
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
58
import com.google.gwt.user.client.ui.HorizontalPanel;
59
import com.google.gwt.user.client.ui.Label;
60
import com.google.gwt.user.client.ui.TextBox;
61
import com.google.gwt.user.client.ui.VerticalPanel;
62

    
63
/**
64
 * The 'File properties' dialog box implementation.
65
 *
66
 */
67
public class FilePublishDialog extends AbstractPropertiesDialog {
68

    
69
        protected CheckBox readForAll;
70
        
71
        private HorizontalPanel pathPanel;
72
        
73
        private TextBox path;
74
        
75
        /**
76
         * An image bundle for this widgets images.
77
         */
78
        public interface Images extends MessagePanel.Images {
79

    
80
                @Source("gr/grnet/pithos/resources/edit_user.png")
81
                ImageResource permUser();
82

    
83
                @Source("gr/grnet/pithos/resources/groups22.png")
84
                ImageResource permGroup();
85

    
86
                @Source("gr/grnet/pithos/resources/editdelete.png")
87
                ImageResource delete();
88
        }
89

    
90
        final File file;
91

    
92
    Images images = GWT.create(Images.class);
93

    
94
        /**
95
         * The widget's constructor.
96
         */
97
        public FilePublishDialog(Pithos _app, File _file) {
98
        super(_app);
99
        file = _file;
100

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

    
115
                // Outer contains inner and buttons.
116
                final VerticalPanel outer = new VerticalPanel();
117
                outer.add(close);
118
                final FocusPanel focusPanel = new FocusPanel(outer);
119
                // Inner contains generalPanel and permPanel.
120
                inner = new VerticalPanel();
121
                inner.addStyleName("inner");
122

    
123
        inner.add(createSharingPanel());
124

    
125
        outer.add(inner);
126

    
127
                final Button ok = new Button("OK", new ClickHandler() {
128
                        @Override
129
                        public void onClick(ClickEvent event) {
130
                                closeDialog();
131
                        }
132
                });
133
                ok.addStyleName("button");
134

    
135
        outer.add(ok);
136
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
137

    
138
        focusPanel.setFocus(true);
139
        setWidget(outer);
140
        }
141

    
142
    private VerticalPanel createSharingPanel() {
143
        VerticalPanel permPanel = new VerticalPanel();
144

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

    
150
        readForAll = new CheckBox();
151
        readForAll.setValue(file.isPublished());
152
        readForAll.addClickHandler(new ClickHandler() {
153
            @Override
154
            public void onClick(ClickEvent event) {
155
                    accept();
156
            }
157
        });
158

    
159
        // Only show the read for all permission if the user is the owner.
160
        if (file.getOwnerID().equals(app.getUserID())) {
161
            final HorizontalPanel permForAll = new HorizontalPanel();
162
            permForAll.add(new Label("Public"));
163
            permForAll.add(readForAll);
164
            permForAll.setSpacing(8);
165
            permForAll.addStyleName("pithos-TabPanelBottom");
166
            permForAll.add(readForAllNote);
167
            permPanel.add(permForAll);
168
        }
169

    
170
        pathPanel = new HorizontalPanel();
171
        pathPanel.setVisible(false);
172
        pathPanel.setWidth("100%");
173
        pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
174
        pathPanel.add(new Label("Link"));
175
        pathPanel.setSpacing(8);
176
        pathPanel.addStyleName("pithos-TabPanelBottom");
177

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

    
195
        Scheduler.get().scheduleDeferred(new Command() {
196
                        
197
                        @Override
198
                        public void execute() {
199
                                showLinkIfPublished();
200
                        }
201
                });
202
        return permPanel;
203
    }
204

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

    
231
        protected void updateMetaData(String api, String owner, final String path, final Boolean published) {
232
        if (published != null) {
233
            PostRequest updateFile = new PostRequest(api, owner, path) {
234
                @Override
235
                public void onSuccess(Resource result) {
236
                        HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), file.getOwnerID(), path, file) {
237

    
238
                                                @Override
239
                                                public void onSuccess(File _result) {
240
                                                        showLinkIfPublished();
241
                                                        if (!app.isMySharedSelected())
242
                                            app.updateFolder(file.getParent(), true, new Command() {
243
                                                                        
244
                                                                        @Override
245
                                                                        public void execute() {
246
                                                                                app.updateMySharedRoot();
247
                                                                        }
248
                                                                }, true);
249
                                                        else
250
                                                                app.updateSharedFolder(file.getParent(), true);
251
                                                }
252

    
253
                                                @Override
254
                                                public void onError(Throwable t) {
255
                                    GWT.log("", t);
256
                                                        app.setError(t);
257
                                    app.displayError("System error modifying file:" + t.getMessage());
258
                                                }
259

    
260
                                                @Override
261
                                                protected void onUnauthorized(Response response) {
262
                                                        app.sessionExpired();
263
                                                }
264
                                        };
265
                                        headFile.setHeader("X-Auth-Token", app.getUserToken());
266
                                        Scheduler.get().scheduleDeferred(headFile);
267
                }
268

    
269
                @Override
270
                public void onError(Throwable t) {
271
                    GWT.log("", t);
272
                                        app.setError(t);
273
                    app.displayError("System error modifying file:" + t.getMessage());
274
                }
275

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

    
298
        @Override
299
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
300
            super.onPreviewNativeEvent(preview);
301

    
302
            NativeEvent evt = preview.getNativeEvent();
303
            if (evt.getType().equals("keydown") && evt.getKeyCode() == KeyCodes.KEY_ENTER)
304
                                closeDialog();
305
        }
306
}