early commit - first attemp, regarding giving a uri and fetching the proper directory
[pithos] / src / gr / ebs / gss / client / FileUploadGearsDialog.java
1 /*\r
2  * Copyright 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import gr.ebs.gss.client.rest.PostCommand;\r
22 import gr.ebs.gss.client.rest.RestCommand;\r
23 import gr.ebs.gss.client.rest.RestException;\r
24 import gr.ebs.gss.client.rest.resource.FileResource;\r
25 import gr.ebs.gss.client.rest.resource.FolderResource;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.Arrays;\r
29 import java.util.HashMap;\r
30 import java.util.List;\r
31 import java.util.Map;\r
32 \r
33 import com.google.gwt.core.client.GWT;\r
34 import com.google.gwt.event.dom.client.ClickEvent;\r
35 import com.google.gwt.event.dom.client.ClickHandler;\r
36 import com.google.gwt.gears.client.Factory;\r
37 import com.google.gwt.gears.client.desktop.Desktop;\r
38 import com.google.gwt.gears.client.desktop.File;\r
39 import com.google.gwt.gears.client.desktop.OpenFilesHandler;\r
40 import com.google.gwt.gears.client.httprequest.HttpRequest;\r
41 import com.google.gwt.gears.client.httprequest.ProgressEvent;\r
42 import com.google.gwt.gears.client.httprequest.ProgressHandler;\r
43 import com.google.gwt.gears.client.httprequest.RequestCallback;\r
44 import com.google.gwt.http.client.URL;\r
45 import com.google.gwt.json.client.JSONObject;\r
46 import com.google.gwt.json.client.JSONString;\r
47 import com.google.gwt.user.client.Command;\r
48 import com.google.gwt.user.client.DeferredCommand;\r
49 import com.google.gwt.user.client.ui.Button;\r
50 import com.google.gwt.user.client.ui.FlexTable;\r
51 import com.google.gwt.user.client.ui.HTML;\r
52 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
53 import com.google.gwt.user.client.ui.HorizontalPanel;\r
54 import com.google.gwt.user.client.ui.VerticalPanel;\r
55 \r
56 /**\r
57  * The 'File upload' dialog box implementation with Google Gears support.\r
58  */\r
59 public class FileUploadGearsDialog extends FileUploadDialog implements Updateable {\r
60 \r
61         protected final Factory factory = Factory.getInstance();\r
62 \r
63         /**\r
64          * The array of files to upload.\r
65          */\r
66         private File[] fileObjects;\r
67 \r
68         /**\r
69          * A list of files to upload, created from files array. Used to signal\r
70          * finished state when empty.\r
71          */\r
72         protected List<File> selectedFiles = new ArrayList<File>();\r
73 \r
74         /**\r
75          * The list of progress bars for individual files.\r
76          */\r
77         protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>();\r
78 \r
79         private Button browse;\r
80 \r
81         private Button submit;\r
82 \r
83         private FlexTable generalTable;\r
84 \r
85         private Map<String, FileResource> toRename;\r
86 \r
87         protected List<HttpRequest> requests = new ArrayList<HttpRequest>();\r
88 \r
89         /**\r
90          * The widget's constructor.\r
91          */\r
92         public FileUploadGearsDialog() {\r
93                 // Set the dialog's caption.\r
94                 setText("File upload");\r
95                 setAnimationEnabled(true);\r
96                 // Create a panel to hold all of the dialog widgets.\r
97                 VerticalPanel panel = new VerticalPanel();\r
98                 final HTML info = new HTML("You may select one or more files to upload.");\r
99                 info.addStyleName("gss-uploadNote");\r
100                 panel.add(info);\r
101                 // Add an informative label with the folder name.\r
102                 Object selection = GSS.get().getFolders().getCurrent().getUserObject();\r
103                 folder = (FolderResource) selection;\r
104 \r
105                 browse = new Button("Browse...");\r
106 \r
107                 HorizontalPanel fileUploadPanel = new HorizontalPanel();\r
108                 fileUploadPanel.add(browse);\r
109 \r
110                 generalTable = new FlexTable();\r
111                 generalTable.setText(0, 0, "Folder");\r
112                 generalTable.setText(1, 0, "File");\r
113                 generalTable.setText(0, 1, folder.getName());\r
114                 generalTable.setWidget(1, 1, fileUploadPanel);\r
115                 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");\r
116                 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");\r
117                 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");\r
118                 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");\r
119                 generalTable.setCellSpacing(4);\r
120 \r
121                 panel.add(generalTable);\r
122 \r
123                 // Create a panel to hold the buttons.\r
124                 HorizontalPanel buttons = new HorizontalPanel();\r
125 \r
126                 submit = new Button("Upload");\r
127                 submit.addClickHandler(new ClickHandler() {\r
128                         @Override\r
129                         public void onClick(ClickEvent event) {\r
130                                 prepareAndSubmit();\r
131                         }\r
132                 });\r
133                 submit.setEnabled(false);\r
134                 buttons.add(submit);\r
135                 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);\r
136                 // Create the 'Cancel' button, along with a listener that hides the\r
137                 // dialog when the button is clicked.\r
138                 Button cancel = new Button("Cancel", new ClickHandler() {\r
139                         @Override\r
140                         public void onClick(ClickEvent event) {\r
141                                 cancelUpload();\r
142                         }\r
143                 });\r
144                 buttons.add(cancel);\r
145                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
146                 buttons.setSpacing(8);\r
147                 buttons.addStyleName("gss-DialogBox");\r
148 \r
149                 browse.addClickHandler(new ClickHandler() {\r
150                         @Override\r
151                         public void onClick(ClickEvent event) {\r
152                                 Desktop desktop = factory.createDesktop();\r
153                                 desktop.openFiles(new OpenFilesHandler() {\r
154 \r
155                                         public void onOpenFiles(OpenFilesEvent ofevent) {\r
156                                                 fileObjects = ofevent.getFiles();\r
157                                                 selectedFiles.addAll(Arrays.asList(fileObjects));\r
158                                                 for (int i = 0; i< selectedFiles.size(); i++) {\r
159                                                         generalTable.setText(i+1, 0, "File");\r
160                                                         generalTable.setText(i+1, 1, selectedFiles.get(i).getName());\r
161                                                         ProgressBar progress = new ProgressBar(20, 0);\r
162                                                         generalTable.setWidget(i+1, 2, progress);\r
163                                                         progressBars.add(progress);\r
164                                                         generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels");\r
165                                                         generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values");\r
166                                                 }\r
167                                                 submit.setEnabled(true);\r
168                                         }\r
169                                 });\r
170                         }\r
171                 });\r
172 \r
173                 panel.add(buttons);\r
174                 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
175                 panel.addStyleName("gss-DialogBox");\r
176                 addStyleName("gss-DialogBox");\r
177                 setWidget(panel);\r
178         }\r
179 \r
180         /**\r
181          * Cancels the file upload.\r
182          */\r
183         private void cancelUpload() {\r
184                 for (HttpRequest request: requests)\r
185                         request.abort();\r
186                 hide();\r
187         }\r
188 \r
189         /**\r
190          * Check whether the specified file name exists in the selected folder.\r
191          */\r
192         private boolean canContinue(File file) {\r
193                 String fileName = getFilename(file.getName());\r
194                 if (getFileForName(fileName) == null)\r
195                         // For file creation, check to see if the file already exists.\r
196                         for (FileResource fileRes : files)\r
197                                 if (!fileRes.isDeleted() && fileRes.getName().equals(fileName))\r
198                                         return false;\r
199                 return true;\r
200         }\r
201 \r
202         @Override\r
203         public void prepareAndSubmit() {\r
204                 GSS app = GSS.get();\r
205                 if (selectedFiles.size() == 0) {\r
206                         app.displayError("You must select a file!");\r
207                         hide();\r
208                         return;\r
209                 }\r
210                 for (File file: selectedFiles)\r
211                         if (!canContinue(file)) {\r
212                                 app.displayError("The file name " + file.getName() +\r
213                                                         " already exists in this folder");\r
214                                 hide();\r
215                                 return;\r
216                         }\r
217                 submit.setEnabled(false);\r
218                 browse.setVisible(false);\r
219                 List<String> toUpdate = new ArrayList<String>();\r
220                 toRename = new HashMap<String, FileResource>();\r
221                 for (File file: selectedFiles) {\r
222                         String fname = getFilename(file.getName());\r
223                         if (getFileForName(fname) == null) {\r
224                                 // We are going to create a file, so we check to see if there is a\r
225                                 // trashed file with the same name.\r
226                                 FileResource same = null;\r
227                                 for (FileResource fres : folder.getFiles())\r
228                                         if (fres.isDeleted() && fres.getName().equals(fname))\r
229                                                 same = fres;\r
230                                 // In that case add it to the list of files to rename.\r
231                                 if (same != null)\r
232                                         toRename.put(getBackupFilename(fname), same);\r
233                         } else\r
234                                 // If we are updating a file add it to the list of files to update.\r
235                                 toUpdate.add(fname);\r
236                 }\r
237 \r
238                 if (!toUpdate.isEmpty()) {\r
239                         StringBuffer sb = new StringBuffer();\r
240                         for (String name: toUpdate)\r
241                                 sb.append(name).append("<br/>");\r
242                         // We are going to update existing files, so show a confirmation dialog.\r
243                         ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +\r
244                                         "you want to update the following files?<br/><i>" + sb +\r
245                                         "</i>", "Update") {\r
246 \r
247                                 @Override\r
248                                 public void cancel() {\r
249                                         hide();\r
250                                 }\r
251 \r
252                                 @Override\r
253                                 public void confirm() {\r
254                                         confirmRename();\r
255                                 }\r
256 \r
257                         };\r
258                         confirm.center();\r
259                 } else\r
260                         confirmRename();\r
261         }\r
262 \r
263         /**\r
264          * Confirm the renames of synonymous files already in the trash.\r
265          */\r
266         private void confirmRename() {\r
267                 if (!toRename.isEmpty()) {\r
268                         StringBuffer sb = new StringBuffer();\r
269                         for (FileResource file: toRename.values())\r
270                                 sb.append(file.getName()).append("<br/>");\r
271                         ConfirmationDialog confirm = new ConfirmationDialog("Files " +\r
272                                         "with the following names already exist in the trash. If" +\r
273                                         " you continue,<br/>the trashed files will be renamed " +\r
274                                         "automatically for you:<br/><i>" + sb + "</i>", "Continue") {\r
275 \r
276                                 @Override\r
277                                 public void cancel() {\r
278                                         hide();\r
279                                 }\r
280 \r
281                                 @Override\r
282                                 public void confirm() {\r
283                                         updateTrashedFiles();\r
284                                 }\r
285 \r
286                         };\r
287                         confirm.center();\r
288                 } else\r
289                         uploadFiles();\r
290         }\r
291 \r
292         /**\r
293          * Rename the conflicting trashed files with the supplied new names.\r
294          */\r
295         private void updateTrashedFiles() {\r
296                 for (final String name: toRename.keySet()) {\r
297                         JSONObject json = new JSONObject();\r
298                         json.put("name", new JSONString(name));\r
299                         PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) {\r
300 \r
301                                 @Override\r
302                                 public void onComplete() {\r
303                                         toRename.remove(name);\r
304                                         uploadFiles();\r
305                                 }\r
306 \r
307                                 @Override\r
308                                 public void onError(Throwable t) {\r
309                                         GSS app = GSS.get();\r
310                                         GWT.log("", t);\r
311                                         if (t instanceof RestException) {\r
312                                                 int statusCode = ((RestException) t).getHttpStatusCode();\r
313                                                 if (statusCode == 405)\r
314                                                         app.displayError("You don't have the necessary permissions");\r
315                                                 else if (statusCode == 404)\r
316                                                         app.displayError("User in permissions does not exist");\r
317                                                 else if (statusCode == 409)\r
318                                                         app.displayError("A file with the same name already exists");\r
319                                                 else if (statusCode == 413)\r
320                                                         app.displayError("Your quota has been exceeded");\r
321                                                 else\r
322                                                         app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
323                                         } else\r
324                                                 app.displayError("System error modifying file:" + t.getMessage());\r
325                                 }\r
326 \r
327                         };\r
328                         DeferredCommand.addCommand(cf);\r
329                 }\r
330         }\r
331 \r
332         /**\r
333          * Checks if the renaming step for already trashed files is complete and\r
334          * starts file uploads.\r
335          */\r
336         private void uploadFiles() {\r
337                 if (!toRename.isEmpty()) return;\r
338                 for (int i = 0; i< fileObjects.length; i++) {\r
339                         final int index = i;\r
340                         DeferredCommand.addCommand(new Command() {\r
341                                 public void execute() {\r
342                                         doSend(fileObjects[index], index);\r
343                                 }\r
344                         });\r
345                 }\r
346         }\r
347 \r
348         /**\r
349          * Perform the HTTP request to upload the specified file.\r
350          */\r
351         protected void doSend(final File file, final int index) {\r
352                 final GSS app = GSS.get();\r
353                 HttpRequest request = factory.createHttpRequest();\r
354                 requests.add(request);\r
355                 String method = "PUT";\r
356 \r
357                 String path;\r
358                 final String filename = getFilename(file.getName());\r
359                 path = folder.getUri();\r
360                 if (!path.endsWith("/"))\r
361                         path = path + "/";\r
362                 path = path + encode(filename);\r
363 \r
364                 String token = app.getToken();\r
365                 String resource = path.substring(app.getApiPath().length()-1, path.length());\r
366                 String date = RestCommand.getDate();\r
367                 String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));\r
368                 request.open(method, path);\r
369                 request.setRequestHeader("X-GSS-Date", date);\r
370                 request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);\r
371                 request.setRequestHeader("Accept", "application/json; charset=utf-8");\r
372                 request.setCallback(new RequestCallback() {\r
373                         public void onResponseReceived(HttpRequest req) {\r
374                                 int state = req.getReadyState();\r
375                                 if (state != 4) return;\r
376                                 switch(req.getStatus()) {\r
377                                         case 201: // Created falls through to updated.\r
378                                         case 204:\r
379                                                 selectedFiles.remove(file);\r
380                                                 finish();\r
381                                                 break;\r
382                                         case 403:\r
383                                                 SessionExpiredDialog dlg = new SessionExpiredDialog();\r
384                                                 dlg.center();\r
385                                                 break;\r
386                                         case 405:\r
387                                                 app.displayError("You don't have permission to " +\r
388                                                                 "upload file " + filename);\r
389                                                 break;\r
390                                         case 409:\r
391                                                 app.displayError("A folder with the name " + filename +\r
392                                                                 " already exists at this level");\r
393                                                 break;\r
394                                         case 413:\r
395                                                 app.displayError("There is not enough free space " +\r
396                                                                 "available for uploading " + filename);\r
397                                                 break;\r
398                                         default:\r
399                                                 app.displayError("Error uploading file " + filename +\r
400                                                                         ": " + req.getStatus());\r
401                                 }\r
402                         }\r
403                 });\r
404                 request.getUpload().setProgressHandler(new ProgressHandler() {\r
405                         public void onProgress(ProgressEvent event) {\r
406                                 double pcnt = (double) event.getLoaded() / event.getTotal();\r
407                                 progressBars.get(index).setProgress((int) Math.floor(pcnt * 100));\r
408                         }\r
409                 });\r
410                 request.send(file.getBlob());\r
411         }\r
412 \r
413         /**\r
414          * Perform the final actions after the files are uploaded.\r
415          */\r
416         protected void finish() {\r
417                 if (!selectedFiles.isEmpty()) return;\r
418                 hide();\r
419                 GSS.get().showFileList(true);\r
420                 GSS.get().getStatusPanel().updateStats();\r
421         }\r
422 \r
423         /**\r
424          * Same as URL.encode, but also encode apostrophe since browsers aren't\r
425          * consistent about it (FF encodes, IE does not).\r
426          */\r
427         protected String encode(String decodedURL) {\r
428                 String retv = decodedURL.replaceAll("@", "_"); // Replace bad character\r
429                 retv = URL.encodeComponent(retv);\r
430                 retv = retv.replaceAll("'", "%27");\r
431                 return retv;\r
432         }\r
433 \r
434 }\r