Add Cut, Move to trash and Delete options to Other's shared context menu.
[pithos] / src / gr / ebs / gss / client / commands / PasteCommand.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.clipboard.Clipboard;
23 import gr.ebs.gss.client.clipboard.ClipboardItem;
24 import gr.ebs.gss.client.dnd.DnDTreeItem;
25 import gr.ebs.gss.client.rest.PostCommand;
26 import gr.ebs.gss.client.rest.RestException;
27 import gr.ebs.gss.client.rest.resource.FileResource;
28 import gr.ebs.gss.client.rest.resource.FolderResource;
29 import gr.ebs.gss.client.rest.resource.GroupResource;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import com.google.gwt.core.client.GWT;
35 import com.google.gwt.http.client.URL;
36 import com.google.gwt.user.client.Command;
37 import com.google.gwt.user.client.DeferredCommand;
38 import com.google.gwt.user.client.ui.PopupPanel;
39 import com.google.gwt.user.client.ui.TreeItem;
40
41 /**
42  * @author kman Command for pasting Clipboard contents
43  */
44 public class PasteCommand implements Command {
45
46         private PopupPanel containerPanel;
47
48         public PasteCommand(PopupPanel _containerPanel) {
49                 containerPanel = _containerPanel;
50         }
51
52         public void execute() {
53                 containerPanel.hide();
54                 Object selection = GSS.get().getCurrentSelection();
55                 if(selection != null && selection instanceof GroupResource){
56                         final ClipboardItem citem = GSS.get().getClipboard().getItem();
57                         GroupResource group = (GroupResource) GSS.get().getCurrentSelection();
58                         if(citem.getUser() != null){
59                                 PostCommand cg = new PostCommand(group.getUri()+"?name="+citem.getUser().getUsername(), "", 201){
60
61                                         @Override
62                                         public void onComplete() {
63                                                 GSS.get().getGroups().updateGroups();
64                                                 GSS.get().showUserList();
65                                         }
66
67                                         @Override
68                                         public void onError(Throwable t) {
69                                                 GWT.log("", t);
70                                                 if(t instanceof RestException){
71                                                         int statusCode = ((RestException)t).getHttpStatusCode();
72                                                         if(statusCode == 405)
73                                                                 GSS.get().displayError("You don't have the necessary permissions");
74                                                         else if(statusCode == 404)
75                                                                 GSS.get().displayError("User does not exist");
76                                                         else if(statusCode == 409)
77                                                                 GSS.get().displayError("A user with the same name already exists");
78                                                         else if(statusCode == 413)
79                                                                 GSS.get().displayError("Your quota has been exceeded");
80                                                         else
81                                                                 GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
82                                                 }
83                                                 else
84                                                         GSS.get().displayError("System error adding user:"+t.getMessage());
85                                         }
86                                 };
87                                 DeferredCommand.addCommand(cg);
88                                 return;
89                         }
90                 }
91                 FolderResource selectedFolder = null;
92                 if(selection != null && selection instanceof FolderResource)
93                         selectedFolder = (FolderResource)selection;
94                 else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
95                         selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
96                 if (selectedFolder != null) {
97                         final ClipboardItem citem = GSS.get().getClipboard().getItem();
98                         if (citem != null && citem.getFolderResource() != null) {
99                                 String target = selectedFolder.getUri();
100                                 target = target.endsWith("/") ? target : target + '/';
101                                 target = target + URL.encodeComponent(citem.getFolderResource().getName());
102                                 if (citem.getOperation() == Clipboard.COPY) {
103                                         PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?copy=" + target, "", 200) {
104
105                                                 @Override
106                                                 public void onComplete() {
107                                                         GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
108                                                         GSS.get().getStatusPanel().updateStats();
109                                                 }
110
111                                                 @Override
112                                                 public void onError(Throwable t) {
113                                                         GWT.log("", t);
114                                                         if(t instanceof RestException){
115                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
116                                                                 if(statusCode == 405)
117                                                                         GSS.get().displayError("You don't have the necessary permissions");
118
119                                                                 else if(statusCode == 409)
120                                                                         GSS.get().displayError("A folder with the same name already exists");
121                                                                 else if(statusCode == 413)
122                                                                         GSS.get().displayError("Your quota has been exceeded");
123                                                                 else
124                                                                         GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
125                                                         }
126                                                         else
127                                                                 GSS.get().displayError("System error copying folder:"+t.getMessage());
128                                                 }
129                                         };
130                                         DeferredCommand.addCommand(cf);
131                                 } else if (citem.getOperation() == Clipboard.CUT) {
132                                         PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?move=" + target, "", 200) {
133
134                                                 @Override
135                                                 public void onComplete() {
136                                                         List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
137                                                         for (TreeItem item : items)
138                                                                 if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
139                                                                         GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
140                                                         GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
141                                                 }
142
143                                                 @Override
144                                                 public void onError(Throwable t) {
145                                                         GWT.log("", t);
146                                                         if(t instanceof RestException){
147                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
148                                                                 if(statusCode == 405)
149                                                                         GSS.get().displayError("You don't have the necessary permissions");
150                                                                 else if(statusCode == 409)
151                                                                         GSS.get().displayError("A folder with the same name already exists");
152                                                                 else if(statusCode == 413)
153                                                                         GSS.get().displayError("Your quota has been exceeded");
154                                                                 else
155                                                                         GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
156                                                         }
157                                                         else
158                                                                 GSS.get().displayError("System error moving folder:"+t.getMessage());
159                                                 }
160                                         };
161                                         DeferredCommand.addCommand(cf);
162                                 }
163                                 return;
164                         } else if (citem != null && citem.getFile() != null) {
165                                 String target = selectedFolder.getUri();
166                                 target = target.endsWith("/") ? target : target + '/';
167                                 target = target + URL.encodeComponent(citem.getFile().getName());
168                                 if (citem.getOperation() == Clipboard.COPY) {
169                                         PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
170
171                                                 @Override
172                                                 public void onComplete() {
173                                                         GSS.get().showFileList(true);
174                                                         GSS.get().getStatusPanel().updateStats();
175                                                 }
176
177                                                 @Override
178                                                 public void onError(Throwable t) {
179                                                         GWT.log("", t);
180                                                         if(t instanceof RestException){
181                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
182                                                                 if(statusCode == 405)
183                                                                         GSS.get().displayError("You don't have the necessary permissions");
184                                                                 else if(statusCode == 404)
185                                                                         GSS.get().displayError("File not found");
186                                                                 else if(statusCode == 409)
187                                                                         GSS.get().displayError("A file with the same name already exists");
188                                                                 else if(statusCode == 413)
189                                                                         GSS.get().displayError("Your quota has been exceeded");
190                                                                 else
191                                                                         GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
192                                                         }
193                                                         else
194                                                                 GSS.get().displayError("System error copying file:"+t.getMessage());
195                                                 }
196                                         };
197                                         DeferredCommand.addCommand(cf);
198                                 } else if (citem.getOperation() == Clipboard.CUT) {
199                                         PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
200
201                                                 @Override
202                                                 public void onComplete() {
203                                                         GSS.get().showFileList(true);
204                                                 }
205
206                                                 @Override
207                                                 public void onError(Throwable t) {
208                                                         GWT.log("", t);
209                                                         if(t instanceof RestException){
210                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
211                                                                 if(statusCode == 405)
212                                                                         GSS.get().displayError("You don't have the necessary permissions");
213                                                                 else if(statusCode == 404)
214                                                                         GSS.get().displayError("File not found");
215                                                                 else if(statusCode == 409)
216                                                                         GSS.get().displayError("A file with the same name already exists");
217                                                                 else if(statusCode == 413)
218                                                                         GSS.get().displayError("Your quota has been exceeded");
219                                                                 else
220                                                                         GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
221                                                         }
222                                                         else
223                                                                 GSS.get().displayError("System error copying file:"+t.getMessage());
224                                                 }
225                                         };
226                                         DeferredCommand.addCommand(cf);
227                                 }
228                                 return;
229                         } else if (citem != null && citem.getFiles() != null) {
230                                 List<FileResource> res = citem.getFiles();
231                                 List<String> fileIds = new ArrayList<String>();
232                                 String target = selectedFolder.getUri();
233                                 target = target.endsWith("/") ? target : target + '/';
234
235                                 if (citem.getOperation() == Clipboard.COPY) {
236                                         for (FileResource fileResource : res) {
237                                                 String fileTarget = target + fileResource.getName();
238                                                 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
239                                         }
240                                         int index = 0;
241                                         executeCopyOrMove(index, fileIds);
242
243                                 } else if (citem.getOperation() == Clipboard.CUT) {
244                                         for (FileResource fileResource : res) {
245                                                 String fileTarget = target + fileResource.getName();
246                                                 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
247                                         }
248                                         int index =0;
249                                         executeCopyOrMove(index, fileIds);
250                                 }
251                                 return;
252                         }
253                 }
254         }
255
256         private void executeCopyOrMove(final int index, final List<String> paths){
257                 if(index >= paths.size()){
258                         GSS.get().showFileList(true);
259                         GSS.get().getStatusPanel().updateStats();
260                         return;
261                 }
262                 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
263
264                         @Override
265                         public void onComplete() {
266                                 executeCopyOrMove(index+1, paths);
267                         }
268
269                         @Override
270                         public void onError(Throwable t) {
271                                 GWT.log("", t);
272                                 if(t instanceof RestException){
273                                         int statusCode = ((RestException)t).getHttpStatusCode();
274                                         if(statusCode == 405)
275                                                 GSS.get().displayError("You don't have the necessary permissions");
276                                         else if(statusCode == 404)
277                                                 GSS.get().displayError("File not found");
278                                         else if(statusCode == 409)
279                                                 GSS.get().displayError("A file with the same name already exists");
280                                         else if(statusCode == 413)
281                                                 GSS.get().displayError("Your quota has been exceeded");
282                                         else
283                                                 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
284                                 }
285                                 else
286                                         GSS.get().displayError("System error copying file:"+t.getMessage());
287                         }
288                 };
289                 DeferredCommand.addCommand(cf);
290         }
291 }