Bump version to 0.16
[pithos-web-client] / src / gr / grnet / pithos / web / client / DeleteFileDialog.java
1 /*\r
2  * Copyright 2011-2013 GRNET S.A. All rights reserved.\r
3  *\r
4  * Redistribution and use in source and binary forms, with or\r
5  * without modification, are permitted provided that the following\r
6  * conditions are met:\r
7  *\r
8  *   1. Redistributions of source code must retain the above\r
9  *      copyright notice, this list of conditions and the following\r
10  *      disclaimer.\r
11  *\r
12  *   2. Redistributions in binary form must reproduce the above\r
13  *      copyright notice, this list of conditions and the following\r
14  *      disclaimer in the documentation and/or other materials\r
15  *      provided with the distribution.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28  * POSSIBILITY OF SUCH DAMAGE.\r
29  *\r
30  * The views and conclusions contained in the software and\r
31  * documentation are those of the authors and should not be\r
32  * interpreted as representing official policies, either expressed\r
33  * or implied, of GRNET S.A.\r
34  */\r
35 package gr.grnet.pithos.web.client;\r
36 \r
37 import com.google.gwt.core.client.GWT;\r
38 import com.google.gwt.core.client.Scheduler;\r
39 import com.google.gwt.dom.client.NativeEvent;\r
40 import com.google.gwt.event.dom.client.ClickEvent;\r
41 import com.google.gwt.event.dom.client.ClickHandler;\r
42 import com.google.gwt.event.dom.client.KeyCodes;\r
43 import com.google.gwt.http.client.Response;\r
44 import com.google.gwt.http.client.URL;\r
45 import com.google.gwt.safehtml.shared.SafeHtmlUtils;\r
46 import com.google.gwt.user.client.Command;\r
47 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
48 import com.google.gwt.user.client.ui.*;\r
49 import gr.grnet.pithos.web.client.MessagePanel.Images;\r
50 import gr.grnet.pithos.web.client.foldertree.File;\r
51 import gr.grnet.pithos.web.client.foldertree.Folder;\r
52 import gr.grnet.pithos.web.client.rest.DeleteRequest;\r
53 import gr.grnet.pithos.web.client.rest.RestException;\r
54 \r
55 import java.util.Iterator;\r
56 import java.util.List;\r
57 \r
58 /**\r
59  * The 'delete file' dialog box.\r
60  */\r
61 public class DeleteFileDialog extends DialogBox {\r
62 \r
63     private List<File> files;\r
64 \r
65     protected Pithos app;\r
66 \r
67         /**\r
68          * The widget's constructor.\r
69          *\r
70          * @param images the supplied images\r
71          */\r
72         public DeleteFileDialog(Pithos _app, Images images, List<File> _files) {\r
73         app = _app;\r
74         files = _files;\r
75                 Anchor close = new Anchor("close");\r
76                 close.addStyleName("close");\r
77                 close.addClickHandler(new ClickHandler() {\r
78                         \r
79                         @Override\r
80                         public void onClick(ClickEvent event) {\r
81                                 hide();\r
82                         }\r
83                 });\r
84                 // Set the dialog's caption.\r
85                 setText("Confirmation");\r
86                 setGlassEnabled(true);\r
87                 setStyleName("pithos-DialogBox");\r
88                 // Create a VerticalPanel to contain the label and the buttons.\r
89                 VerticalPanel outer = new VerticalPanel();\r
90                 outer.add(close);\r
91                 \r
92                 VerticalPanel inner = new VerticalPanel();\r
93                 inner.addStyleName("inner");\r
94 \r
95                 HTML text;\r
96         final String fileName = files.get(0).getName();\r
97         final String safeFileName = SafeHtmlUtils.htmlEscape(fileName);\r
98                 if (files.size() == 1) {\r
99             text = new HTML("<table><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete file '" + safeFileName + "'?</td></tr></table>");\r
100         }\r
101                 else {\r
102             text = new HTML("<table><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + "Are you sure you want to <b>permanently</b> delete the selected files?</td></tr></table>");\r
103         }\r
104                 text.setStyleName("pithos-warnMessage");\r
105                 inner.add(text);\r
106 \r
107                 // Create the 'Delete' button, along with a listener that hides the dialog\r
108                 // when the button is clicked and deletes the file.\r
109                 Button ok = new Button("Delete", new ClickHandler() {\r
110                         @Override\r
111                         public void onClick(ClickEvent event) {\r
112                                 deleteFiles();\r
113                                 hide();\r
114                         }\r
115                 });\r
116                 ok.addStyleName("button");\r
117                 inner.add(ok);\r
118                 inner.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);\r
119                 outer.add(inner);\r
120                 outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);\r
121                 setWidget(outer);\r
122         }\r
123 \r
124         /**\r
125          * Generate an RPC request to delete a file.\r
126          */\r
127         protected void deleteFiles() {\r
128         Iterator<File> iter = files.iterator();\r
129         deleteFile(iter);\r
130     }\r
131 \r
132         protected void deleteFile(final Iterator<File> iter) {\r
133         if (iter.hasNext()) {\r
134             File f = iter.next();\r
135             String path = f.getUri();\r
136             DeleteRequest deleteFile = new DeleteRequest(Pithos.getStorageAPIURL(), f.getOwnerID(), URL.encode(path)) {\r
137                 @Override\r
138                 public void onSuccess(Resource result) {\r
139                     deleteFile(iter);\r
140                 }\r
141 \r
142                 @Override\r
143                 public void onError(Throwable t) {\r
144                     GWT.log("", t);\r
145                                         app.setError(t);\r
146                     if (t instanceof RestException) {\r
147                         app.displayError("Unable to delete file: " + ((RestException) t).getHttpStatusText());\r
148                     }\r
149                     else\r
150                         app.displayError("System error unable to delete file: "+t.getMessage());\r
151                     deleteFile(iter);\r
152                 }\r
153 \r
154                                 @Override\r
155                                 protected void onUnauthorized(Response response) {\r
156                                         app.sessionExpired();\r
157                                 }\r
158             };\r
159             deleteFile.setHeader("X-Auth-Token", app.getUserToken());\r
160             Scheduler.get().scheduleDeferred(deleteFile);\r
161         }\r
162         else {\r
163                 Folder f = files.get(0).getParent();\r
164                 if (app.isMySharedSelected())\r
165                         app.updateSharedFolder(f, true, new Command() {\r
166                                         \r
167                                         @Override\r
168                                         public void execute() {\r
169                                                 app.updateStatistics();\r
170                                         }\r
171                                 });\r
172                 else\r
173                     app.updateFolder(files.get(0).getParent(), true, new Command() {\r
174                                         \r
175                                         @Override\r
176                                         public void execute() {\r
177                                                 app.updateStatistics();\r
178                                         }\r
179                                 }, true);\r
180         }\r
181     }\r
182 \r
183         @Override\r
184         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
185                 super.onPreviewNativeEvent(preview);\r
186 \r
187                 NativeEvent evt = preview.getNativeEvent();\r
188                 if (evt.getType().equals("keydown"))\r
189                         // Use the popup's key preview hooks to close the dialog when either\r
190                         // enter or escape is pressed.\r
191                         switch (evt.getKeyCode()) {\r
192                                 case KeyCodes.KEY_ENTER:\r
193                                         hide();\r
194                                         deleteFiles();\r
195                                         break;\r
196                                 case KeyCodes.KEY_ESCAPE:\r
197                                         hide();\r
198                                         break;\r
199                         }\r
200         }\r
201 \r
202 \r
203 \r
204 }\r