Properly cancel the upload for both gears-enabled and regular file uploads. This...
[pithos] / src / gr / ebs / gss / client / commands / ToTrashCommand.java
1 /*
2  * Copyright 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client.commands;
20
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.dnd.DnDTreeItem;
23 import gr.ebs.gss.client.rest.MultiplePostCommand;
24 import gr.ebs.gss.client.rest.PostCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FileResource;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import com.google.gwt.core.client.GWT;
33 import com.google.gwt.user.client.Command;
34 import com.google.gwt.user.client.DeferredCommand;
35 import com.google.gwt.user.client.ui.PopupPanel;
36 import com.google.gwt.user.client.ui.TreeItem;
37
38 /**
39  *
40  * Move file or folder to trash.
41  *
42  * @author kman
43  *
44  */
45 public class ToTrashCommand implements Command{
46         private PopupPanel containerPanel;
47
48         public ToTrashCommand(PopupPanel _containerPanel){
49                 containerPanel = _containerPanel;
50         }
51
52         public void execute() {
53                 containerPanel.hide();
54                 Object selection = GSS.get().getCurrentSelection();
55                 if (selection == null)
56                         return;
57                 GWT.log("selection: " + selection.toString(), null);
58                 if (selection instanceof FolderResource) {
59                         FolderResource fdto = (FolderResource) selection;
60                         PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
61
62                                 @Override
63                                 public void onComplete() {
64                                         TreeItem folder = GSS.get().getFolders().getCurrent();
65                                         if(folder.getParentItem() != null){
66                                                 GSS.get().getFolders().select(folder.getParentItem());
67                                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder.getParentItem());
68                                         }
69                                         GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
70                                         GSS.get().showFileList(true);
71                                 }
72
73                                 @Override
74                                 public void onError(Throwable t) {
75                                         GWT.log("", t);
76                                         if(t instanceof RestException){
77                                                 int statusCode = ((RestException)t).getHttpStatusCode();
78                                                 if(statusCode == 405)
79                                                         GSS.get().displayError("You don't have the necessary permissions");
80                                                 else if(statusCode == 404)
81                                                         GSS.get().displayError("Folder does not exist");
82                                                 else
83                                                         GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
84                                         }
85                                         else
86                                                 GSS.get().displayError("System error trashing folder:"+t.getMessage());
87                                 }
88                         };
89                         DeferredCommand.addCommand(tot);
90                 } else if (selection instanceof FileResource) {
91                         FileResource fdto = (FileResource) selection;
92                         PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
93
94                                 @Override
95                                 public void onComplete() {
96                                         GSS.get().showFileList(true);
97                                 }
98
99                                 @Override
100                                 public void onError(Throwable t) {
101                                         GWT.log("", t);
102                                         if(t instanceof RestException){
103                                                 int statusCode = ((RestException)t).getHttpStatusCode();
104                                                 if(statusCode == 405)
105                                                         GSS.get().displayError("You don't have the necessary permissions");
106                                                 else if(statusCode == 404)
107                                                         GSS.get().displayError("File does not exist");
108                                                 else
109                                                         GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
110                                         }
111                                         else
112                                                 GSS.get().displayError("System error trashing file:"+t.getMessage());
113                                 }
114                         };
115                         DeferredCommand.addCommand(tot);
116
117                 }
118                 else if (selection instanceof List) {
119                         List<FileResource> fdtos = (List<FileResource>) selection;
120                         final List<String> fileIds = new ArrayList<String>();
121                         for(FileResource f : fdtos)
122                                 fileIds.add(f.getUri()+"?trash=");
123                         MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
124
125                                 @Override
126                                 public void onComplete() {
127                                         GSS.get().showFileList(true);
128                                 }
129
130                                 @Override
131                                 public void onError(String p, Throwable t) {
132                                         GWT.log("", t);
133                                         if(t instanceof RestException){
134                                                 int statusCode = ((RestException)t).getHttpStatusCode();
135                                                 if(statusCode == 405)
136                                                         GSS.get().displayError("You don't have the necessary permissions");
137                                                 else if(statusCode == 404)
138                                                         GSS.get().displayError("File does not exist");
139                                                 else
140                                                         GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
141                                         }
142                                         else
143                                                 GSS.get().displayError("System error trashing file:"+t.getMessage());
144                                 }
145                         };
146                         DeferredCommand.addCommand(tot);
147                 }
148         }
149
150 }