Improved rounding for human-readable percentOfFreeSpace().
[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.user.client.Command;
35 import com.google.gwt.user.client.DeferredCommand;
36 import com.google.gwt.user.client.ui.MenuBar;
37 import com.google.gwt.user.client.ui.PopupPanel;
38 import com.google.gwt.user.client.ui.TreeItem;
39
40 /**
41  * @author kman
42  */
43 public class DnDFolderPopupMenu extends PopupPanel {
44
45         /**
46          * The widget's images.
47          */
48         private final Folders.Images images;
49
50         public DnDFolderPopupMenu(final Folders.Images newImages, final FolderResource target, final Object toCopy, boolean othersShared) {
51                 // The popup's constructor's argument is a boolean specifying that it
52                 // auto-close itself when the user clicks outside of it.
53                 super(true);
54                 setAnimationEnabled(true);
55                 images = newImages;
56
57                 // A dummy command that we will execute from unimplemented leaves.
58                 final Command cancelCmd = new Command() {
59
60                         public void execute() {
61                                 hide();
62                         }
63                 };
64
65                 final MenuBar contextMenu = new MenuBar(true);
66                 final Folders folders = GSS.get().getFolders();
67
68                 if (!othersShared)
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                         public void onComplete() {
129                                 final TreeItem folder;
130                                 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
131                                 if (folderTemp == null)
132                                         folder = GSS.get().getFolders().getOtherSharedItem(target);
133                                 else
134                                         folder = folderTemp;
135                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
136                         }
137
138                         public void onError(Throwable t) {
139                                 GWT.log("", t);
140                                 if (t instanceof RestException) {
141                                         int statusCode = ((RestException) t).getHttpStatusCode();
142                                         if (statusCode == 405)
143                                                 GSS.get().displayError("You don't have the necessary permissions");
144
145                                         else if (statusCode == 409)
146                                                 GSS.get().displayError("A folder with the same name already exists");
147                                         else if (statusCode == 413)
148                                                 GSS.get().displayError("Your quota has been exceeded");
149                                         else
150                                                 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
151                                 } else
152                                         GSS.get().displayError("System error copying folder:" + t.getMessage());
153                         }
154                 };
155                 DeferredCommand.addCommand(cf);
156         }
157
158         private void moveFolder(final FolderResource target, final FolderResource toCopy, final List<TreeItem> items) {
159                 String atarget = target.getUri();
160                 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
161                 atarget = atarget + toCopy.getName();
162
163                 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
164
165                         public void onComplete() {
166                                 final TreeItem folder;
167                                 for(TreeItem i : items){
168                                         DnDTreeItem id = (DnDTreeItem)i;
169                                         if(id.getChild(toCopy) != null)
170                                                 id.removeItem(id.getChild(toCopy));
171                                 }
172                                 GSS.get().getFolders().clearSelection();
173                                 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
174                                 if (folderTemp == null)
175                                         folder = GSS.get().getFolders().getOtherSharedItem(target);
176                                 else
177                                         folder = folderTemp;
178                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
179                                 GSS.get().showFileList(true);
180                         }
181
182                         public void onError(Throwable t) {
183                                 GWT.log("", t);
184                                 if (t instanceof RestException) {
185                                         int statusCode = ((RestException) t).getHttpStatusCode();
186                                         if (statusCode == 405)
187                                                 GSS.get().displayError("You don't have the necessary permissions");
188
189                                         else if (statusCode == 409)
190                                                 GSS.get().displayError("A folder with the same name already exists");
191                                         else if (statusCode == 413)
192                                                 GSS.get().displayError("Your quota has been exceeded");
193                                         else
194                                                 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
195                                 } else
196                                         GSS.get().displayError("System error copying folder:" + t.getMessage());
197                         }
198                 };
199                 DeferredCommand.addCommand(cf);
200         }
201
202         private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
203                 List<String> fileIds = new ArrayList<String>();
204                 String target = ftarget.getUri();
205                 target = target.endsWith("/") ? target : target + '/';
206                 for (FileResource fileResource : files) {
207                         String fileTarget = target + fileResource.getName();
208                         fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
209                 }
210                 int index = 0;
211                 executeCopyOrMoveFiles(index, fileIds);
212
213         }
214
215         private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
216                 List<String> fileIds = new ArrayList<String>();
217                 String target = ftarget.getUri();
218                 target = target.endsWith("/") ? target : target + '/';
219                 for (FileResource fileResource : files) {
220                         String fileTarget = target + fileResource.getName();
221                         fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
222                 }
223                 int index = 0;
224                 executeCopyOrMoveFiles(index, fileIds);
225
226         }
227
228         private void trashFolder(final FolderResource folder, final List<TreeItem> items){
229                 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
230
231                         public void onComplete() {
232                                 for(TreeItem item : items)
233                                         GSS.get().getFolders().updateFolder((DnDTreeItem) item);
234                                 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
235                                 GSS.get().showFileList(true);
236                         }
237
238                         public void onError(Throwable t) {
239                                 GWT.log("", t);
240                                 if(t instanceof RestException){
241                                         int statusCode = ((RestException)t).getHttpStatusCode();
242                                         if(statusCode == 405)
243                                                 GSS.get().displayError("You don't have the necessary permissions");
244                                         else if(statusCode == 404)
245                                                 GSS.get().displayError("Folder does not exist");
246                                         else
247                                                 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
248                                 }
249                                 else
250                                         GSS.get().displayError("System error trashing folder:"+t.getMessage());
251                         }
252                 };
253                 DeferredCommand.addCommand(tot);
254         }
255
256         private void trashFiles(List<FileResource> files){
257                 final List<String> fileIds = new ArrayList<String>();
258                 for(FileResource f : files)
259                         fileIds.add(f.getUri()+"?trash=");
260                 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
261
262                         public void onComplete() {
263                                 GSS.get().showFileList(true);
264                         }
265
266
267                         public void onError(String p, Throwable t) {
268                                 GWT.log("", t);
269                                 if(t instanceof RestException){
270                                         int statusCode = ((RestException)t).getHttpStatusCode();
271                                         if(statusCode == 405)
272                                                 GSS.get().displayError("You don't have the necessary permissions");
273                                         else if(statusCode == 404)
274                                                 GSS.get().displayError("File does not exist");
275                                         else
276                                                 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
277                                 }
278                                 else
279                                         GSS.get().displayError("System error trashing file:"+t.getMessage());
280                         }
281                 };
282                 DeferredCommand.addCommand(tot);
283         }
284
285
286         private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
287                 if (index >= paths.size()) {
288                         GSS.get().showFileList(true);
289                         return;
290                 }
291                 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
292
293                         @Override
294                         public void onComplete() {
295                                 executeCopyOrMoveFiles(index + 1, paths);
296                         }
297
298                         @Override
299                         public void onError(Throwable t) {
300                                 GWT.log("", t);
301                                 if (t instanceof RestException) {
302                                         int statusCode = ((RestException) t).getHttpStatusCode();
303                                         if (statusCode == 405)
304                                                 GSS.get().displayError("You don't have the necessary permissions");
305                                         else if (statusCode == 404)
306                                                 GSS.get().displayError("File not found");
307                                         else if (statusCode == 409)
308                                                 GSS.get().displayError("A file with the same name already exists");
309                                         else if (statusCode == 413)
310                                                 GSS.get().displayError("Your quota has been exceeded");
311                                         else
312                                                 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
313                                 } else
314                                         GSS.get().displayError("System error copying file:" + t.getMessage());
315
316                         }
317                 };
318                 DeferredCommand.addCommand(cf);
319         }
320
321 }