Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / FileUploadGearsDialog.java @ 4d3dcdf6

History | View | Annotate | Download (15.1 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
36 ab1eb3f8 Christos Stathis
37 26cbae7a Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
38 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostCommand;
39 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestCommand;
40 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
41 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FileResource;
42 ab1eb3f8 Christos Stathis
43 ab1eb3f8 Christos Stathis
import java.util.ArrayList;
44 ab1eb3f8 Christos Stathis
import java.util.Arrays;
45 ab1eb3f8 Christos Stathis
import java.util.HashMap;
46 ab1eb3f8 Christos Stathis
import java.util.List;
47 ab1eb3f8 Christos Stathis
import java.util.Map;
48 ab1eb3f8 Christos Stathis
49 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
51 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
52 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.Factory;
53 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.desktop.Desktop;
54 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.desktop.File;
55 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.desktop.OpenFilesHandler;
56 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.httprequest.HttpRequest;
57 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.httprequest.ProgressEvent;
58 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.httprequest.ProgressHandler;
59 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.httprequest.RequestCallback;
60 ab1eb3f8 Christos Stathis
import com.google.gwt.http.client.URL;
61 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONObject;
62 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONString;
63 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
64 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Button;
65 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
66 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HTML;
67 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
68 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
69 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
70 ab1eb3f8 Christos Stathis
71 ab1eb3f8 Christos Stathis
/**
72 ab1eb3f8 Christos Stathis
 * The 'File upload' dialog box implementation with Google Gears support.
73 ab1eb3f8 Christos Stathis
 */
74 26cbae7a Christos Stathis
public class FileUploadGearsDialog extends FileUploadDialog {
75 ab1eb3f8 Christos Stathis
76 ab1eb3f8 Christos Stathis
        protected final Factory factory = Factory.getInstance();
77 ab1eb3f8 Christos Stathis
78 ab1eb3f8 Christos Stathis
        /**
79 ab1eb3f8 Christos Stathis
         * The array of files to upload.
80 ab1eb3f8 Christos Stathis
         */
81 ab1eb3f8 Christos Stathis
        private File[] fileObjects;
82 ab1eb3f8 Christos Stathis
83 ab1eb3f8 Christos Stathis
        /**
84 ab1eb3f8 Christos Stathis
         * A list of files to upload, created from files array. Used to signal
85 ab1eb3f8 Christos Stathis
         * finished state when empty.
86 ab1eb3f8 Christos Stathis
         */
87 ab1eb3f8 Christos Stathis
        protected List<File> selectedFiles = new ArrayList<File>();
88 ab1eb3f8 Christos Stathis
89 ab1eb3f8 Christos Stathis
        /**
90 ab1eb3f8 Christos Stathis
         * The list of progress bars for individual files.
91 ab1eb3f8 Christos Stathis
         */
92 ab1eb3f8 Christos Stathis
        protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>();
93 ab1eb3f8 Christos Stathis
94 ab1eb3f8 Christos Stathis
        private Button browse;
95 ab1eb3f8 Christos Stathis
96 ab1eb3f8 Christos Stathis
        private Button submit;
97 ab1eb3f8 Christos Stathis
98 ab1eb3f8 Christos Stathis
        private FlexTable generalTable;
99 ab1eb3f8 Christos Stathis
100 26cbae7a Christos Stathis
        private Map<String, gr.grnet.pithos.web.client.foldertree.File> toRename;
101 ab1eb3f8 Christos Stathis
102 ab1eb3f8 Christos Stathis
        protected List<HttpRequest> requests = new ArrayList<HttpRequest>();
103 ab1eb3f8 Christos Stathis
        
104 ab1eb3f8 Christos Stathis
        private boolean canContinue = true;
105 ab1eb3f8 Christos Stathis
106 26cbae7a Christos Stathis
    protected FileUploadGearsDialog() {
107 26cbae7a Christos Stathis
    }
108 26cbae7a Christos Stathis
109 ab1eb3f8 Christos Stathis
        /**
110 ab1eb3f8 Christos Stathis
         * The widget's constructor.
111 ab1eb3f8 Christos Stathis
         */
112 26cbae7a Christos Stathis
        public FileUploadGearsDialog(GSS _app, Folder _folder) {
113 26cbae7a Christos Stathis
        this.folder = _folder;
114 26cbae7a Christos Stathis
        this.app = _app;
115 ab1eb3f8 Christos Stathis
                // Set the dialog's caption.
116 ab1eb3f8 Christos Stathis
                setText("File upload");
117 ab1eb3f8 Christos Stathis
                setAnimationEnabled(true);
118 ab1eb3f8 Christos Stathis
                // Create a panel to hold all of the dialog widgets.
119 ab1eb3f8 Christos Stathis
                VerticalPanel panel = new VerticalPanel();
120 ab1eb3f8 Christos Stathis
                final HTML info = new HTML("You may select one or more files to upload.");
121 ab1eb3f8 Christos Stathis
                info.addStyleName("pithos-uploadNote");
122 ab1eb3f8 Christos Stathis
                panel.add(info);
123 ab1eb3f8 Christos Stathis
                // Add an informative label with the folder name.
124 ab1eb3f8 Christos Stathis
                Object selection = GSS.get().getTreeView().getSelection();
125 ab1eb3f8 Christos Stathis
126 ab1eb3f8 Christos Stathis
                browse = new Button("Browse...");
127 ab1eb3f8 Christos Stathis
128 ab1eb3f8 Christos Stathis
                HorizontalPanel fileUploadPanel = new HorizontalPanel();
129 ab1eb3f8 Christos Stathis
                fileUploadPanel.add(browse);
130 ab1eb3f8 Christos Stathis
131 ab1eb3f8 Christos Stathis
                generalTable = new FlexTable();
132 ab1eb3f8 Christos Stathis
                generalTable.setText(0, 0, "Folder");
133 ab1eb3f8 Christos Stathis
                generalTable.setText(1, 0, "File");
134 ab1eb3f8 Christos Stathis
                generalTable.setText(0, 1, folder.getName());
135 ab1eb3f8 Christos Stathis
                generalTable.setWidget(1, 1, fileUploadPanel);
136 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
137 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
138 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
139 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
140 ab1eb3f8 Christos Stathis
                generalTable.setCellSpacing(4);
141 ab1eb3f8 Christos Stathis
142 ab1eb3f8 Christos Stathis
                panel.add(generalTable);
143 ab1eb3f8 Christos Stathis
144 ab1eb3f8 Christos Stathis
                // Create a panel to hold the buttons.
145 ab1eb3f8 Christos Stathis
                HorizontalPanel buttons = new HorizontalPanel();
146 ab1eb3f8 Christos Stathis
147 ab1eb3f8 Christos Stathis
                submit = new Button("Upload");
148 ab1eb3f8 Christos Stathis
                submit.addClickHandler(new ClickHandler() {
149 ab1eb3f8 Christos Stathis
                        @Override
150 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
151 ab1eb3f8 Christos Stathis
                                prepareAndSubmit();
152 ab1eb3f8 Christos Stathis
                        }
153 ab1eb3f8 Christos Stathis
                });
154 ab1eb3f8 Christos Stathis
                submit.setEnabled(false);
155 ab1eb3f8 Christos Stathis
                buttons.add(submit);
156 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
157 ab1eb3f8 Christos Stathis
                // Create the 'Cancel' button, along with a listener that hides the
158 ab1eb3f8 Christos Stathis
                // dialog when the button is clicked.
159 ab1eb3f8 Christos Stathis
                Button cancel = new Button("Cancel", new ClickHandler() {
160 ab1eb3f8 Christos Stathis
                        @Override
161 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
162 ab1eb3f8 Christos Stathis
                                canContinue = false;                                
163 ab1eb3f8 Christos Stathis
                                cancelUpload();                                
164 ab1eb3f8 Christos Stathis
                                GSS.get().showFileList(true);
165 ab1eb3f8 Christos Stathis
                        }
166 ab1eb3f8 Christos Stathis
                });
167 ab1eb3f8 Christos Stathis
                buttons.add(cancel);
168 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
169 ab1eb3f8 Christos Stathis
                buttons.setSpacing(8);
170 ab1eb3f8 Christos Stathis
                buttons.addStyleName("pithos-DialogBox");
171 ab1eb3f8 Christos Stathis
172 ab1eb3f8 Christos Stathis
                browse.addClickHandler(new ClickHandler() {
173 ab1eb3f8 Christos Stathis
                        @Override
174 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
175 ab1eb3f8 Christos Stathis
                                Desktop desktop = factory.createDesktop();
176 ab1eb3f8 Christos Stathis
                                desktop.openFiles(new OpenFilesHandler() {
177 ab1eb3f8 Christos Stathis
178 ab1eb3f8 Christos Stathis
                                        @Override
179 ab1eb3f8 Christos Stathis
                                        public void onOpenFiles(OpenFilesEvent ofevent) {
180 ab1eb3f8 Christos Stathis
                                                fileObjects = ofevent.getFiles();
181 ab1eb3f8 Christos Stathis
                                                selectedFiles.addAll(Arrays.asList(fileObjects));
182 ab1eb3f8 Christos Stathis
                                                for (int i = 0; i< selectedFiles.size(); i++) {
183 ab1eb3f8 Christos Stathis
                                                        generalTable.setText(i+1, 0, "File");
184 ab1eb3f8 Christos Stathis
                                                        generalTable.setText(i+1, 1, selectedFiles.get(i).getName());
185 ab1eb3f8 Christos Stathis
                                                        ProgressBar progress = new ProgressBar(20, 0);
186 ab1eb3f8 Christos Stathis
                                                        generalTable.setWidget(i+1, 2, progress);
187 ab1eb3f8 Christos Stathis
                                                        progressBars.add(progress);
188 ab1eb3f8 Christos Stathis
                                                        generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels");
189 ab1eb3f8 Christos Stathis
                                                        generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values");
190 ab1eb3f8 Christos Stathis
                                                }
191 ab1eb3f8 Christos Stathis
                                                submit.setEnabled(true);
192 ab1eb3f8 Christos Stathis
                                        }
193 ab1eb3f8 Christos Stathis
                                });
194 ab1eb3f8 Christos Stathis
                        }
195 ab1eb3f8 Christos Stathis
                });
196 ab1eb3f8 Christos Stathis
197 ab1eb3f8 Christos Stathis
                panel.add(buttons);
198 ab1eb3f8 Christos Stathis
                panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
199 ab1eb3f8 Christos Stathis
                panel.addStyleName("pithos-DialogBox");
200 ab1eb3f8 Christos Stathis
                addStyleName("pithos-DialogBox");
201 ab1eb3f8 Christos Stathis
                setWidget(panel);
202 ab1eb3f8 Christos Stathis
        }
203 ab1eb3f8 Christos Stathis
204 ab1eb3f8 Christos Stathis
        /**
205 ab1eb3f8 Christos Stathis
         * Cancels the file upload.
206 ab1eb3f8 Christos Stathis
         */
207 ab1eb3f8 Christos Stathis
        private void cancelUpload() {
208 ab1eb3f8 Christos Stathis
                for (HttpRequest request: requests)
209 ab1eb3f8 Christos Stathis
                        request.abort();
210 ab1eb3f8 Christos Stathis
                hide();                
211 ab1eb3f8 Christos Stathis
        }
212 ab1eb3f8 Christos Stathis
213 ab1eb3f8 Christos Stathis
        @Override
214 ab1eb3f8 Christos Stathis
        public void prepareAndSubmit() {
215 ab1eb3f8 Christos Stathis
                GSS app = GSS.get();
216 ab1eb3f8 Christos Stathis
                if (selectedFiles.size() == 0) {
217 ab1eb3f8 Christos Stathis
                        app.displayError("You must select a file!");
218 ab1eb3f8 Christos Stathis
                        hide();
219 ab1eb3f8 Christos Stathis
                        return;
220 ab1eb3f8 Christos Stathis
                }
221 ab1eb3f8 Christos Stathis
                submit.setEnabled(false);
222 ab1eb3f8 Christos Stathis
                browse.setVisible(false);
223 ab1eb3f8 Christos Stathis
                List<String> toUpdate = new ArrayList<String>();
224 26cbae7a Christos Stathis
                toRename = new HashMap<String, gr.grnet.pithos.web.client.foldertree.File>();
225 ab1eb3f8 Christos Stathis
                for (File file: selectedFiles) {
226 ab1eb3f8 Christos Stathis
                        String fname = getFilename(file.getName());
227 ab1eb3f8 Christos Stathis
                        if (getFileForName(fname) == null) {
228 ab1eb3f8 Christos Stathis
                                // We are going to create a file, so we check to see if there is a
229 ab1eb3f8 Christos Stathis
                                // trashed file with the same name.
230 26cbae7a Christos Stathis
                                gr.grnet.pithos.web.client.foldertree.File same = null;
231 26cbae7a Christos Stathis
                                for (gr.grnet.pithos.web.client.foldertree.File fres : folder.getFiles())
232 26cbae7a Christos Stathis
                                        if (fres.isInTrash() && fres.getName().equals(fname))
233 ab1eb3f8 Christos Stathis
                                                same = fres;
234 ab1eb3f8 Christos Stathis
                                // In that case add it to the list of files to rename.
235 ab1eb3f8 Christos Stathis
                                if (same != null)
236 ab1eb3f8 Christos Stathis
                                        toRename.put(getBackupFilename(fname), same);
237 ab1eb3f8 Christos Stathis
                        } else
238 ab1eb3f8 Christos Stathis
                                // If we are updating a file add it to the list of files to update.
239 ab1eb3f8 Christos Stathis
                                toUpdate.add(fname);
240 ab1eb3f8 Christos Stathis
                }
241 ab1eb3f8 Christos Stathis
242 ab1eb3f8 Christos Stathis
                if (!toUpdate.isEmpty()) {
243 ab1eb3f8 Christos Stathis
                        StringBuffer sb = new StringBuffer();
244 ab1eb3f8 Christos Stathis
                        for (String name: toUpdate)
245 ab1eb3f8 Christos Stathis
                                sb.append(name).append("<br/>");
246 ab1eb3f8 Christos Stathis
                        // We are going to update existing files, so show a confirmation dialog.
247 ab1eb3f8 Christos Stathis
                        ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
248 ab1eb3f8 Christos Stathis
                                        "you want to update the following files?<br/><i>" + sb +
249 ab1eb3f8 Christos Stathis
                                        "</i>", "Update") {
250 ab1eb3f8 Christos Stathis
251 ab1eb3f8 Christos Stathis
                                @Override
252 ab1eb3f8 Christos Stathis
                                public void cancel() {
253 ab1eb3f8 Christos Stathis
                                        hide();
254 ab1eb3f8 Christos Stathis
                                }
255 ab1eb3f8 Christos Stathis
256 ab1eb3f8 Christos Stathis
                                @Override
257 ab1eb3f8 Christos Stathis
                                public void confirm() {
258 ab1eb3f8 Christos Stathis
                                        confirmRename();
259 ab1eb3f8 Christos Stathis
                                }
260 ab1eb3f8 Christos Stathis
261 ab1eb3f8 Christos Stathis
                        };
262 ab1eb3f8 Christos Stathis
                        confirm.center();
263 ab1eb3f8 Christos Stathis
                } else
264 ab1eb3f8 Christos Stathis
                        confirmRename();
265 ab1eb3f8 Christos Stathis
        }
266 ab1eb3f8 Christos Stathis
267 ab1eb3f8 Christos Stathis
        /**
268 ab1eb3f8 Christos Stathis
         * Confirm the renames of synonymous files already in the trash.
269 ab1eb3f8 Christos Stathis
         */
270 ab1eb3f8 Christos Stathis
        private void confirmRename() {
271 ab1eb3f8 Christos Stathis
                if (!toRename.isEmpty()) {
272 ab1eb3f8 Christos Stathis
                        StringBuffer sb = new StringBuffer();
273 26cbae7a Christos Stathis
                        for (gr.grnet.pithos.web.client.foldertree.File file: toRename.values())
274 ab1eb3f8 Christos Stathis
                                sb.append(file.getName()).append("<br/>");
275 ab1eb3f8 Christos Stathis
                        ConfirmationDialog confirm = new ConfirmationDialog("Files " +
276 ab1eb3f8 Christos Stathis
                                        "with the following names already exist in the trash. If" +
277 ab1eb3f8 Christos Stathis
                                        " you continue,<br/>the trashed files will be renamed " +
278 ab1eb3f8 Christos Stathis
                                        "automatically for you:<br/><i>" + sb + "</i>", "Continue") {
279 ab1eb3f8 Christos Stathis
280 ab1eb3f8 Christos Stathis
                                @Override
281 ab1eb3f8 Christos Stathis
                                public void cancel() {
282 ab1eb3f8 Christos Stathis
                                        hide();
283 ab1eb3f8 Christos Stathis
                                }
284 ab1eb3f8 Christos Stathis
285 ab1eb3f8 Christos Stathis
                                @Override
286 ab1eb3f8 Christos Stathis
                                public void confirm() {
287 ab1eb3f8 Christos Stathis
                                        updateTrashedFiles();
288 ab1eb3f8 Christos Stathis
                                }
289 ab1eb3f8 Christos Stathis
290 ab1eb3f8 Christos Stathis
                        };
291 ab1eb3f8 Christos Stathis
                        confirm.center();
292 ab1eb3f8 Christos Stathis
                } else
293 ab1eb3f8 Christos Stathis
                        uploadFiles();
294 ab1eb3f8 Christos Stathis
        }
295 ab1eb3f8 Christos Stathis
296 ab1eb3f8 Christos Stathis
        /**
297 ab1eb3f8 Christos Stathis
         * Rename the conflicting trashed files with the supplied new names.
298 ab1eb3f8 Christos Stathis
         */
299 ab1eb3f8 Christos Stathis
        private void updateTrashedFiles() {
300 ab1eb3f8 Christos Stathis
                for (final String name: toRename.keySet()) {
301 ab1eb3f8 Christos Stathis
                        JSONObject json = new JSONObject();
302 ab1eb3f8 Christos Stathis
                        json.put("name", new JSONString(name));
303 ab1eb3f8 Christos Stathis
                        PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) {
304 ab1eb3f8 Christos Stathis
305 ab1eb3f8 Christos Stathis
                                @Override
306 ab1eb3f8 Christos Stathis
                                public void onComplete() {
307 ab1eb3f8 Christos Stathis
                                        toRename.remove(name);
308 ab1eb3f8 Christos Stathis
                                        uploadFiles();
309 ab1eb3f8 Christos Stathis
                                }
310 ab1eb3f8 Christos Stathis
311 ab1eb3f8 Christos Stathis
                                @Override
312 ab1eb3f8 Christos Stathis
                                public void onError(Throwable t) {
313 ab1eb3f8 Christos Stathis
                                        GSS app = GSS.get();
314 ab1eb3f8 Christos Stathis
                                        GWT.log("", t);
315 ab1eb3f8 Christos Stathis
                                        if (t instanceof RestException) {
316 ab1eb3f8 Christos Stathis
                                                int statusCode = ((RestException) t).getHttpStatusCode();
317 ab1eb3f8 Christos Stathis
                                                if (statusCode == 405)
318 ab1eb3f8 Christos Stathis
                                                        app.displayError("You don't have the necessary permissions");
319 ab1eb3f8 Christos Stathis
                                                else if (statusCode == 404)
320 ab1eb3f8 Christos Stathis
                                                        app.displayError("User in permissions does not exist");
321 ab1eb3f8 Christos Stathis
                                                else if (statusCode == 409)
322 ab1eb3f8 Christos Stathis
                                                        app.displayError("A file with the same name already exists");
323 ab1eb3f8 Christos Stathis
                                                else if (statusCode == 413)
324 ab1eb3f8 Christos Stathis
                                                        app.displayError("Your quota has been exceeded");
325 ab1eb3f8 Christos Stathis
                                                else
326 ab1eb3f8 Christos Stathis
                                                        app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
327 ab1eb3f8 Christos Stathis
                                        } else
328 ab1eb3f8 Christos Stathis
                                                app.displayError("System error modifying file:" + t.getMessage());
329 ab1eb3f8 Christos Stathis
                                }
330 ab1eb3f8 Christos Stathis
331 ab1eb3f8 Christos Stathis
                        };
332 ab1eb3f8 Christos Stathis
                        DeferredCommand.addCommand(cf);
333 ab1eb3f8 Christos Stathis
                }
334 ab1eb3f8 Christos Stathis
        }
335 ab1eb3f8 Christos Stathis
336 ab1eb3f8 Christos Stathis
        /**
337 ab1eb3f8 Christos Stathis
         * Checks if the renaming step for already trashed files is complete and
338 ab1eb3f8 Christos Stathis
         * starts file uploads.
339 ab1eb3f8 Christos Stathis
         */
340 ab1eb3f8 Christos Stathis
        private void uploadFiles() {                
341 ab1eb3f8 Christos Stathis
                if (!toRename.isEmpty()) return;
342 ab1eb3f8 Christos Stathis
                if (canContinue){                                                
343 ab1eb3f8 Christos Stathis
                        doSend(selectedFiles);
344 ab1eb3f8 Christos Stathis
                }
345 ab1eb3f8 Christos Stathis
        }
346 ab1eb3f8 Christos Stathis
347 ab1eb3f8 Christos Stathis
        /**
348 ab1eb3f8 Christos Stathis
         * Perform the HTTP request to upload the specified file.
349 ab1eb3f8 Christos Stathis
         */
350 ab1eb3f8 Christos Stathis
        protected void doSend(final List<File> filesRemaining) {
351 ab1eb3f8 Christos Stathis
                final GSS app = GSS.get();
352 ab1eb3f8 Christos Stathis
                HttpRequest request = factory.createHttpRequest();
353 ab1eb3f8 Christos Stathis
                requests.add(request);
354 ab1eb3f8 Christos Stathis
                String method = "PUT";
355 ab1eb3f8 Christos Stathis
356 ab1eb3f8 Christos Stathis
                String path;
357 ab1eb3f8 Christos Stathis
                final String filename = getFilename(filesRemaining.get(0).getName());
358 ab1eb3f8 Christos Stathis
                path = folder.getUri();
359 ab1eb3f8 Christos Stathis
                if (!path.endsWith("/"))
360 ab1eb3f8 Christos Stathis
                        path = path + "/";
361 ab1eb3f8 Christos Stathis
                path = path + encode(filename);
362 ab1eb3f8 Christos Stathis
363 ab1eb3f8 Christos Stathis
                String token = app.getToken();
364 ab1eb3f8 Christos Stathis
                String resource = path.substring(app.getApiPath().length()-1, path.length());
365 ab1eb3f8 Christos Stathis
                String date = RestCommand.getDate();
366 ab1eb3f8 Christos Stathis
                String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
367 ab1eb3f8 Christos Stathis
                request.open(method, path);
368 ab1eb3f8 Christos Stathis
                request.setRequestHeader("X-GSS-Date", date);
369 ab1eb3f8 Christos Stathis
                request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);
370 ab1eb3f8 Christos Stathis
                request.setRequestHeader("Accept", "application/json; charset=utf-8");
371 ab1eb3f8 Christos Stathis
                request.setCallback(new RequestCallback() {
372 ab1eb3f8 Christos Stathis
                        @Override
373 ab1eb3f8 Christos Stathis
                        public void onResponseReceived(HttpRequest req) {
374 ab1eb3f8 Christos Stathis
                                int state = req.getReadyState();
375 ab1eb3f8 Christos Stathis
                                if (state != 4) return;
376 ab1eb3f8 Christos Stathis
                                switch(req.getStatus()) {
377 ab1eb3f8 Christos Stathis
                                        case 201: // Created falls through to updated.
378 ab1eb3f8 Christos Stathis
                                        case 204:
379 ab1eb3f8 Christos Stathis
                                                filesRemaining.remove(0);
380 ab1eb3f8 Christos Stathis
                                                if(filesRemaining.isEmpty()){                                                        
381 ab1eb3f8 Christos Stathis
                                                        finish();
382 ab1eb3f8 Christos Stathis
                                                        break;
383 ab1eb3f8 Christos Stathis
                                                }                                                
384 ab1eb3f8 Christos Stathis
                                                doSend(filesRemaining);                                
385 ab1eb3f8 Christos Stathis
                                                break;
386 ab1eb3f8 Christos Stathis
                                        case 403:
387 ab1eb3f8 Christos Stathis
                                                SessionExpiredDialog dlg = new SessionExpiredDialog();
388 ab1eb3f8 Christos Stathis
                                                dlg.center();
389 ab1eb3f8 Christos Stathis
                                                break;
390 ab1eb3f8 Christos Stathis
                                        case 405:
391 ab1eb3f8 Christos Stathis
                                                app.displayError("You don't have permission to " +
392 ab1eb3f8 Christos Stathis
                                                                "upload file " + filename);
393 ab1eb3f8 Christos Stathis
                                                break;
394 ab1eb3f8 Christos Stathis
                                        case 409:
395 ab1eb3f8 Christos Stathis
                                                app.displayError("A folder with the name " + filename +
396 ab1eb3f8 Christos Stathis
                                                                " already exists at this level");
397 ab1eb3f8 Christos Stathis
                                                break;
398 ab1eb3f8 Christos Stathis
                                        case 413:
399 ab1eb3f8 Christos Stathis
                                                app.displayError("There is not enough free space " +
400 ab1eb3f8 Christos Stathis
                                                                "available for uploading " + filename);
401 ab1eb3f8 Christos Stathis
                                                break;
402 ab1eb3f8 Christos Stathis
                                        default:
403 ab1eb3f8 Christos Stathis
                                                app.displayError("Error uploading file " + filename +
404 ab1eb3f8 Christos Stathis
                                                                        ": " + req.getStatus());
405 ab1eb3f8 Christos Stathis
                                }
406 ab1eb3f8 Christos Stathis
                        }
407 ab1eb3f8 Christos Stathis
                });
408 ab1eb3f8 Christos Stathis
                request.getUpload().setProgressHandler(new ProgressHandler() {
409 ab1eb3f8 Christos Stathis
                        @Override
410 ab1eb3f8 Christos Stathis
                        public void onProgress(ProgressEvent event) {
411 ab1eb3f8 Christos Stathis
                                double pcnt = (double) event.getLoaded() / event.getTotal();
412 ab1eb3f8 Christos Stathis
                                progressBars.get(0).setProgress((int) Math.floor(pcnt * 100));
413 ab1eb3f8 Christos Stathis
                                if(pcnt*100 == 100)
414 ab1eb3f8 Christos Stathis
                                        progressBars.remove(0);
415 ab1eb3f8 Christos Stathis
                        }
416 ab1eb3f8 Christos Stathis
                });
417 ab1eb3f8 Christos Stathis
                request.send(filesRemaining.get(0).getBlob());
418 ab1eb3f8 Christos Stathis
        }
419 ab1eb3f8 Christos Stathis
420 ab1eb3f8 Christos Stathis
        /**
421 ab1eb3f8 Christos Stathis
         * Perform the final actions after the files are uploaded.
422 ab1eb3f8 Christos Stathis
         */
423 ab1eb3f8 Christos Stathis
        protected void finish() {
424 ab1eb3f8 Christos Stathis
                hide();
425 ab1eb3f8 Christos Stathis
                //GSS.get().showFileList(true);
426 ab1eb3f8 Christos Stathis
                GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());//showFileList(true);
427 ab1eb3f8 Christos Stathis
                GSS.get().getStatusPanel().updateStats();
428 ab1eb3f8 Christos Stathis
        }
429 ab1eb3f8 Christos Stathis
430 ab1eb3f8 Christos Stathis
        /**
431 ab1eb3f8 Christos Stathis
         * Same as URL.encode, but also encode apostrophe since browsers aren't
432 ab1eb3f8 Christos Stathis
         * consistent about it (FF encodes, IE does not).
433 ab1eb3f8 Christos Stathis
         */
434 ab1eb3f8 Christos Stathis
        protected String encode(String decodedURL) {
435 ab1eb3f8 Christos Stathis
                String retv = decodedURL.replaceAll("@", "_"); // Replace bad character
436 ab1eb3f8 Christos Stathis
                retv = URL.encodeComponent(retv);
437 ab1eb3f8 Christos Stathis
                retv = retv.replaceAll("'", "%27");
438 ab1eb3f8 Christos Stathis
                return retv;
439 ab1eb3f8 Christos Stathis
        }
440 ab1eb3f8 Christos Stathis
441 26cbae7a Christos Stathis
    protected String getBackupFilename(String filename) {
442 26cbae7a Christos Stathis
        List<gr.grnet.pithos.web.client.foldertree.File> filesInSameFolder = new ArrayList<gr.grnet.pithos.web.client.foldertree.File>();
443 26cbae7a Christos Stathis
        for (gr.grnet.pithos.web.client.foldertree.File deleted : folder.getFiles())
444 26cbae7a Christos Stathis
            if (deleted.isInTrash())
445 26cbae7a Christos Stathis
                filesInSameFolder.add(deleted);
446 26cbae7a Christos Stathis
        int i = 1;
447 26cbae7a Christos Stathis
        for (gr.grnet.pithos.web.client.foldertree.File same : filesInSameFolder)
448 26cbae7a Christos Stathis
            if (same.getName().startsWith(filename)) {
449 26cbae7a Christos Stathis
                String toCheck = same.getName().substring(filename.length(), same.getName().length());
450 26cbae7a Christos Stathis
                if (toCheck.startsWith(" ")) {
451 26cbae7a Christos Stathis
                    int test = -1;
452 26cbae7a Christos Stathis
                    try {
453 26cbae7a Christos Stathis
                        test = Integer.valueOf(toCheck.replace(" ", ""));
454 26cbae7a Christos Stathis
                    } catch (NumberFormatException e) {
455 26cbae7a Christos Stathis
                        // Do nothing since string is not a number.
456 26cbae7a Christos Stathis
                    }
457 26cbae7a Christos Stathis
                    if (test >= i)
458 26cbae7a Christos Stathis
                        i = test + 1;
459 26cbae7a Christos Stathis
                }
460 26cbae7a Christos Stathis
            }
461 26cbae7a Christos Stathis
462 26cbae7a Christos Stathis
        return filename + " " + i;
463 26cbae7a Christos Stathis
    }
464 ab1eb3f8 Christos Stathis
}