Fixed problems copying/moving via drag-and-drop had with international characters...
[pithos] / src / gr / ebs / gss / client / dnd / DnDFolderPopupMenu.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.dnd;
20
21 import gr.ebs.gss.client.Folders;
22 import gr.ebs.gss.client.GSS;
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 import gr.ebs.gss.client.rest.resource.RestResource;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.http.client.URL;
35 import com.google.gwt.user.client.Command;
36 import com.google.gwt.user.client.DeferredCommand;
37 import com.google.gwt.user.client.ui.MenuBar;
38 import com.google.gwt.user.client.ui.PopupPanel;
39 import com.google.gwt.user.client.ui.TreeItem;
40
41 /**
42  * @author kman
43  */
44 public class DnDFolderPopupMenu extends PopupPanel {
45
46         /**
47          * The widget's images.
48          */
49         private final Folders.Images images;
50
51         public DnDFolderPopupMenu(final Folders.Images newImages, final FolderResource target, final Object toCopy, boolean othersShared) {
52                 // The popup's constructor's argument is a boolean specifying that it
53                 // auto-close itself when the user clicks outside of it.
54                 super(true);
55                 setAnimationEnabled(true);
56                 images = newImages;
57
58                 // A dummy command that we will execute from unimplemented leaves.
59                 final Command cancelCmd = new Command() {
60
61                         public void execute() {
62                                 hide();
63                         }
64                 };
65
66                 final MenuBar contextMenu = new MenuBar(true);
67                 final Folders folders = GSS.get().getFolders();
68
69                 contextMenu.addItem("<span>" + newImages.cut().getHTML() + "&nbsp;Move</span>", true, new Command() {
70
71                                 public void execute() {
72                                         if (toCopy instanceof FolderResource){
73                                                 List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
74                                                 List<TreeItem> parents = new ArrayList();
75                                                 for(TreeItem item : treeItems)
76                                                         if(item.getParentItem() != null)
77                                                                 parents.add(item.getParentItem());
78                                                 moveFolder(target, (FolderResource) toCopy, parents);
79                                         }
80                                         else if(toCopy instanceof List)
81                                                 moveFiles(target, (List<FileResource>) toCopy);
82                                         hide();
83                                 }
84
85                         }).setVisible(target != null);
86
87                 contextMenu.addItem("<span>" + newImages.copy().getHTML() + "&nbsp;Copy</span>", true, new Command() {
88
89                         public void execute() {
90                                 if (toCopy instanceof FolderResource)
91                                         copyFolder(target, (FolderResource) toCopy);
92                                 else if(toCopy instanceof List)
93                                         copyFiles(target, (List<FileResource>) toCopy);
94                                 hide();
95                         }
96
97                 }).setVisible(target != null);
98
99                 contextMenu.addItem("<span>" + newImages.trash().getHTML() + "&nbsp;Delete (Trash)</span>", true, new Command() {
100
101                         public void execute() {
102                                 if (toCopy instanceof FolderResource){
103                                         final List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
104                                         List<TreeItem> parents = new ArrayList();
105                                         for(TreeItem item : treeItems)
106                                                 if(item.getParentItem() != null)
107                                                         parents.add(item.getParentItem());
108                                         trashFolder((FolderResource) toCopy, parents);
109                                 }
110                                 else if(toCopy instanceof List)
111                                         trashFiles((List<FileResource>) toCopy);
112                                 hide();
113                         }
114
115                 }).setVisible(target == null);
116                 contextMenu.addItem("<span>" + newImages.delete().getHTML() + "&nbsp;Cancel</span>", true, cancelCmd);
117
118                 add(contextMenu);
119
120         }
121
122         private void copyFolder(final FolderResource target, FolderResource toCopy) {
123                 String atarget = target.getUri();
124                 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
125                 atarget = atarget + toCopy.getName();
126                 PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
127
128                         @Override
129                         public void onComplete() {
130                                 final TreeItem folder;
131                                 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
132                                 if (folderTemp == null)
133                                         folder = GSS.get().getFolders().getOtherSharedItem(target);
134                                 else
135                                         folder = folderTemp;
136                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
137                                 GSS.get().getStatusPanel().updateStats();
138                         }
139
140                         @Override
141                         public void onError(Throwable t) {
142                                 GWT.log("", t);
143                                 if (t instanceof RestException) {
144                                         int statusCode = ((RestException) t).getHttpStatusCode();
145                                         if (statusCode == 405)
146                                                 GSS.get().displayError("You don't have the necessary permissions");
147
148                                         else if (statusCode == 409)
149                                                 GSS.get().displayError("A folder with the same name already exists");
150                                         else if (statusCode == 413)
151                                                 GSS.get().displayError("Your quota has been exceeded");
152                                         else
153                                                 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
154                                 } else
155                                         GSS.get().displayError("System error copying folder:" + t.getMessage());
156                         }
157                 };
158                 DeferredCommand.addCommand(cf);
159         }
160
161         private void moveFolder(final FolderResource target, final FolderResource toCopy, final List<TreeItem> items) {
162                 String atarget = target.getUri();
163                 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
164                 atarget = atarget + toCopy.getName();
165
166                 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
167
168                         @Override
169                         public void onComplete() {
170                                 final TreeItem folder;
171                                 for(TreeItem i : items){
172                                         DnDTreeItem id = (DnDTreeItem)i;
173                                         if(id.getChild(toCopy) != null)
174                                                 id.removeItem(id.getChild(toCopy));
175                                 }
176                                 GSS.get().getFolders().clearSelection();
177                                 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
178                                 if (folderTemp == null)
179                                         folder = GSS.get().getFolders().getOtherSharedItem(target);
180                                 else
181                                         folder = folderTemp;
182                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
183                                 GSS.get().showFileList(true);
184                                 GSS.get().getStatusPanel().updateStats();
185                         }
186
187                         @Override
188                         public void onError(Throwable t) {
189                                 GWT.log("", t);
190                                 if (t instanceof RestException) {
191                                         int statusCode = ((RestException) t).getHttpStatusCode();
192                                         if (statusCode == 405)
193                                                 GSS.get().displayError("You don't have the necessary permissions");
194
195                                         else if (statusCode == 409)
196                                                 GSS.get().displayError("A folder with the same name already exists");
197                                         else if (statusCode == 413)
198                                                 GSS.get().displayError("Your quota has been exceeded");
199                                         else
200                                                 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
201                                 } else
202                                         GSS.get().displayError("System error copying folder:" + t.getMessage());
203                         }
204                 };
205                 DeferredCommand.addCommand(cf);
206         }
207
208         private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
209                 List<String> fileIds = new ArrayList<String>();
210                 String target = ftarget.getUri();
211                 target = target.endsWith("/") ? target : target + '/';
212                 for (FileResource fileResource : files) {
213                         String fileTarget = target + URL.encodeComponent(fileResource.getName());
214                         fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
215                 }
216                 int index = 0;
217                 executeCopyOrMoveFiles(index, fileIds);
218
219         }
220
221         private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
222                 List<String> fileIds = new ArrayList<String>();
223                 String target = ftarget.getUri();
224                 target = target.endsWith("/") ? target : target + '/';
225                 for (FileResource fileResource : files) {
226                         String fileTarget = target + URL.encodeComponent(fileResource.getName());
227                         fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
228                 }
229                 int index = 0;
230                 executeCopyOrMoveFiles(index, fileIds);
231
232         }
233
234         private void trashFolder(final FolderResource folder, final List<TreeItem> items){
235                 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
236
237                         @Override
238                         public void onComplete() {
239                                 for(TreeItem item : items)
240                                         GSS.get().getFolders().updateFolder((DnDTreeItem) item);
241                                 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
242                                 GSS.get().showFileList(true);
243                         }
244
245                         @Override
246                         public void onError(Throwable t) {
247                                 GWT.log("", t);
248                                 if(t instanceof RestException){
249                                         int statusCode = ((RestException)t).getHttpStatusCode();
250                                         if(statusCode == 405)
251                                                 GSS.get().displayError("You don't have the necessary permissions");
252                                         else if(statusCode == 404)
253                                                 GSS.get().displayError("Folder does not exist");
254                                         else
255                                                 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
256                                 }
257                                 else
258                                         GSS.get().displayError("System error trashing folder:"+t.getMessage());
259                         }
260                 };
261                 DeferredCommand.addCommand(tot);
262         }
263
264         private void trashFiles(List<FileResource> files){
265                 final List<String> fileIds = new ArrayList<String>();
266                 for(FileResource f : files)
267                         fileIds.add(f.getUri()+"?trash=");
268                 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
269
270                         @Override
271                         public void onComplete() {
272                                 GSS.get().showFileList(true);
273                         }
274
275                         @Override
276                         public void onError(String p, Throwable t) {
277                                 GWT.log("", t);
278                                 if(t instanceof RestException){
279                                         int statusCode = ((RestException)t).getHttpStatusCode();
280                                         if(statusCode == 405)
281                                                 GSS.get().displayError("You don't have the necessary permissions");
282                                         else if(statusCode == 404)
283                                                 GSS.get().displayError("File does not exist");
284                                         else
285                                                 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
286                                 }
287                                 else
288                                         GSS.get().displayError("System error trashing file:"+t.getMessage());
289                         }
290                 };
291                 DeferredCommand.addCommand(tot);
292         }
293
294
295         private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
296                 if (index >= paths.size()) {
297                         GSS.get().showFileList(true);
298                         GSS.get().getStatusPanel().updateStats();
299                         return;
300                 }
301                 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
302
303                         @Override
304                         public void onComplete() {
305                                 executeCopyOrMoveFiles(index + 1, paths);
306                         }
307
308                         @Override
309                         public void onError(Throwable t) {
310                                 GWT.log("", t);
311                                 if (t instanceof RestException) {
312                                         int statusCode = ((RestException) t).getHttpStatusCode();
313                                         if (statusCode == 405)
314                                                 GSS.get().displayError("You don't have the necessary permissions");
315                                         else if (statusCode == 404)
316                                                 GSS.get().displayError("File not found");
317                                         else if (statusCode == 409)
318                                                 GSS.get().displayError("A file with the same name already exists");
319                                         else if (statusCode == 413)
320                                                 GSS.get().displayError("Your quota has been exceeded");
321                                         else
322                                                 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
323                                 } else
324                                         GSS.get().displayError("System error copying file:" + t.getMessage());
325
326                         }
327                 };
328                 DeferredCommand.addCommand(cf);
329         }
330
331 }