Automated merge with https://gss.googlecode.com/hg/
[pithos] / src / gr / ebs / gss / client / FileMenu.java
1 /*
2  * Copyright 2007, 2008, 2009, 2010 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;
20
21 import gr.ebs.gss.client.commands.EmptyTrashCommand;
22 import gr.ebs.gss.client.commands.NewFolderCommand;
23 import gr.ebs.gss.client.commands.PropertiesCommand;
24 import gr.ebs.gss.client.commands.RefreshCommand;
25 import gr.ebs.gss.client.commands.UploadFileCommand;
26 import gr.ebs.gss.client.rest.RestCommand;
27 import gr.ebs.gss.client.rest.resource.FileResource;
28 import gr.ebs.gss.client.rest.resource.GroupUserResource;
29
30 import java.util.List;
31
32 import com.google.gwt.event.dom.client.ClickEvent;
33 import com.google.gwt.event.dom.client.ClickHandler;
34 import com.google.gwt.http.client.URL;
35 import com.google.gwt.resources.client.ClientBundle;
36 import com.google.gwt.resources.client.ImageResource;
37 import com.google.gwt.user.client.Command;
38 import com.google.gwt.user.client.ui.AbstractImagePrototype;
39 import com.google.gwt.user.client.ui.MenuBar;
40 import com.google.gwt.user.client.ui.PopupPanel;
41 import com.google.gwt.user.client.ui.TreeItem;
42
43 /**
44  * The 'File' menu implementation.
45  */
46 public class FileMenu extends PopupPanel implements ClickHandler {
47
48         /**
49          * The widget's images.
50          */
51         private final Images images;
52
53         /**
54          * An image bundle for this widgets images.
55          */
56         public interface Images extends ClientBundle,FilePropertiesDialog.Images {
57
58                 @Source("gr/ebs/gss/resources/folder_new.png")
59                 ImageResource folderNew();
60
61                 @Source("gr/ebs/gss/resources/folder_outbox.png")
62                 ImageResource fileUpdate();
63
64                 @Source("gr/ebs/gss/resources/view_text.png")
65                 ImageResource viewText();
66
67                 @Override
68                 @Source("gr/ebs/gss/resources/folder_inbox.png")
69                 ImageResource download();
70
71                 @Source("gr/ebs/gss/resources/trashcan_empty.png")
72                 ImageResource emptyTrash();
73
74                 @Source("gr/ebs/gss/resources/internet.png")
75                 ImageResource sharing();
76
77                 @Source("gr/ebs/gss/resources/refresh.png")
78                 ImageResource refresh();
79 }
80
81         final MenuBar contextMenu = new MenuBar(true);
82
83         /**
84          * The widget's constructor.
85          *
86          * @param _images the image bundle passed on by the parent object
87          */
88         public FileMenu(final Images _images) {
89                 // The popup's constructor's argument is a boolean specifying that it
90                 // auto-close itself when the user clicks outside of it.
91                 super(true);
92                 setAnimationEnabled(true);
93                 images = _images;
94                 add(contextMenu);
95
96         }
97
98         @Override
99         public void onClick(ClickEvent event) {
100                 final FileMenu menu = new FileMenu(images);
101                 final int left = event.getRelativeElement().getAbsoluteLeft();
102                 final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
103                 menu.setPopupPosition(left, top);
104                 menu.show();
105
106         }
107
108
109         /**
110          * Do some validation before downloading a file.
111          */
112         void preDownloadCheck() {
113                 Object selection = GSS.get().getCurrentSelection();
114                 if (selection == null || !(selection instanceof FileResource)) {
115                         GSS.get().displayError("You have to select a file first");
116                         return;
117                 }
118         }
119
120         /**
121          * Create a download link for the respective menu item, if the currently
122          * selected object is a file.
123          *
124          * @param link a String array with two elements that is modified so that the
125          *            first position contains the opening tag and the second one the
126          *            closing tag
127          * @param forceDownload If true, link will be such that browser should ask for filename
128          *                              and save location
129          */
130         void createDownloadLink(String[] link, boolean forceDownload) {
131                 String downloadURL = getDownloadURL();
132                 if (!downloadURL.isEmpty()) {
133                         link[0] = "<a class='hidden-link' href='" + downloadURL
134                                         + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
135                         link[1] = "</a>";
136                 }
137         }
138
139         public String getDownloadURL() {
140                 GSS app = GSS.get();
141                 Object selection = app.getCurrentSelection();
142                 if (selection != null && selection instanceof FileResource) {
143                         FileResource file = (FileResource) selection;
144                         return getDownloadURL(file);
145                 }
146                 return "";
147         }
148
149         public String getDownloadURL(FileResource file) {
150                 GSS app = GSS.get();
151                 if (file != null) {
152                         String dateString = RestCommand.getDate();
153                         String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());
154                         String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
155                         return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
156                 }
157                 return "";
158         }
159
160         public MenuBar createMenu() {
161                 contextMenu.clearItems();
162                 contextMenu.setAutoOpen(false);
163                 final Command downloadCmd = new Command() {
164
165                         @Override
166                         public void execute() {
167                                 hide();
168                                 preDownloadCheck();
169                         }
170                 };
171                 Folders folders = GSS.get().getFolders();
172                 TreeItem selectedItem = folders.getCurrent();
173                 boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
174                 boolean propertiesVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource || GSS.get().getCurrentSelection() instanceof List));
175                 boolean newFolderVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem) || folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
176                 boolean uploadVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem)|| folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
177                 if(newFolderVisible)
178                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(this, images));
179                 if(uploadVisible)
180                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
181                 if (downloadVisible) {
182                         String[] link = {"", ""};
183                         createDownloadLink(link, false);
184                         contextMenu.addItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);
185                         createDownloadLink(link, true);
186                         contextMenu.addItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Save As" + link[1] + "</span>", true, downloadCmd);
187                 }
188                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));
189                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
190                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1))
191                                         .setVisible(propertiesVisible);
192                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0))
193                                         .setVisible(propertiesVisible);
194                 return contextMenu;
195         }
196
197 }