Fixed refresh errors when moving folders/files to trash
[pithos-web-client] / src / gr / grnet / pithos / web / client / FilePropertiesDialog.java
1 /*\r
2  * Copyright 2011 GRNET S.A. All rights reserved.\r
3  *\r
4  * Redistribution and use in source and binary forms, with or\r
5  * without modification, are permitted provided that the following\r
6  * conditions are met:\r
7  *\r
8  *   1. Redistributions of source code must retain the above\r
9  *      copyright notice, this list of conditions and the following\r
10  *      disclaimer.\r
11  *\r
12  *   2. Redistributions in binary form must reproduce the above\r
13  *      copyright notice, this list of conditions and the following\r
14  *      disclaimer in the documentation and/or other materials\r
15  *      provided with the distribution.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28  * POSSIBILITY OF SUCH DAMAGE.\r
29  *\r
30  * The views and conclusions contained in the software and\r
31  * documentation are those of the authors and should not be\r
32  * interpreted as representing official policies, either expressed\r
33  * or implied, of GRNET S.A.\r
34  */\r
35 package gr.grnet.pithos.web.client;\r
36 \r
37 import gr.grnet.pithos.web.client.foldertree.File;\r
38 import gr.grnet.pithos.web.client.foldertree.Resource;\r
39 import gr.grnet.pithos.web.client.rest.PostRequest;\r
40 import gr.grnet.pithos.web.client.rest.PutRequest;\r
41 import gr.grnet.pithos.web.client.tagtree.Tag;\r
42 \r
43 import java.util.Map;\r
44 \r
45 import com.google.gwt.core.client.GWT;\r
46 import com.google.gwt.core.client.Scheduler;\r
47 import com.google.gwt.event.dom.client.ClickEvent;\r
48 import com.google.gwt.event.dom.client.ClickHandler;\r
49 import com.google.gwt.i18n.client.DateTimeFormat;\r
50 import com.google.gwt.resources.client.ImageResource;\r
51 import com.google.gwt.user.client.Window;\r
52 import com.google.gwt.user.client.ui.Anchor;\r
53 import com.google.gwt.user.client.ui.Button;\r
54 import com.google.gwt.user.client.ui.CheckBox;\r
55 import com.google.gwt.user.client.ui.DecoratedTabPanel;\r
56 import com.google.gwt.user.client.ui.DisclosurePanel;\r
57 import com.google.gwt.user.client.ui.FlexTable;\r
58 import com.google.gwt.user.client.ui.FlowPanel;\r
59 import com.google.gwt.user.client.ui.FocusPanel;\r
60 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
61 import com.google.gwt.user.client.ui.HorizontalPanel;\r
62 import com.google.gwt.user.client.ui.Label;\r
63 import com.google.gwt.user.client.ui.TextBox;\r
64 import com.google.gwt.user.client.ui.VerticalPanel;\r
65 \r
66 /**\r
67  * The 'File properties' dialog box implementation.\r
68  *\r
69  */\r
70 public class FilePropertiesDialog extends AbstractPropertiesDialog {\r
71 \r
72         protected PermissionsList permList;\r
73 \r
74         protected CheckBox readForAll;\r
75 \r
76         /**\r
77          * An image bundle for this widgets images.\r
78          */\r
79         public interface Images extends MessagePanel.Images {\r
80 \r
81                 @Source("gr/grnet/pithos/resources/edit_user.png")\r
82                 ImageResource permUser();\r
83 \r
84                 @Source("gr/grnet/pithos/resources/groupevent.png")\r
85                 ImageResource permGroup();\r
86 \r
87                 @Source("gr/grnet/pithos/resources/editdelete.png")\r
88                 ImageResource delete();\r
89 \r
90                 @Source("gr/grnet/pithos/resources/db_update.png")\r
91                 ImageResource restore();\r
92 \r
93                 @Source("gr/grnet/pithos/resources/folder_inbox.png")\r
94                 ImageResource download();\r
95         }\r
96 \r
97         /**\r
98          * The widget that holds the name of the file.\r
99          */\r
100         private TextBox name = new TextBox();\r
101 \r
102         final File file;\r
103 \r
104     Images images = GWT.create(Images.class);\r
105 \r
106         /**\r
107          * The widget's constructor.\r
108          */\r
109         public FilePropertiesDialog(Pithos _app, File _file) {\r
110         super(_app);\r
111         file = _file;\r
112 \r
113                 // Set the dialog's caption.\r
114                 setText("File properties");\r
115 \r
116                 // Outer contains inner and buttons.\r
117                 final VerticalPanel outer = new VerticalPanel();\r
118                 final FocusPanel focusPanel = new FocusPanel(outer);\r
119                 // Inner contains generalPanel and permPanel.\r
120                 inner = new DecoratedTabPanel();\r
121                 inner.setAnimationEnabled(true);\r
122 \r
123 \r
124         inner.add(createGeneralPanel(), "General");\r
125 \r
126         inner.add(createSharingPanel(), "Sharing");\r
127 \r
128         inner.add(createVersionPanel(), "Versions");\r
129 \r
130         inner.selectTab(0);\r
131 \r
132         outer.add(inner);\r
133 \r
134                 final HorizontalPanel buttons = new HorizontalPanel();\r
135                 // Create the 'OK' button, along with a listener that hides the dialog\r
136                 // when the button is clicked.\r
137                 final Button ok = new Button("OK", new ClickHandler() {\r
138                         @Override\r
139                         public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
140                                 accept();\r
141                                 closeDialog();\r
142                         }\r
143                 });\r
144 \r
145                 buttons.add(ok);\r
146                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
147                 // Create the 'Cancel' button, along with a listener that hides the\r
148                 // dialog when the button is clicked.\r
149                 final Button cancel = new Button("Cancel", new ClickHandler() {\r
150                         @Override\r
151                         public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
152                                 closeDialog();\r
153                         }\r
154                 });\r
155                 buttons.add(cancel);\r
156                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
157                 buttons.setSpacing(8);\r
158                 buttons.addStyleName("pithos-TabPanelBottom");\r
159 \r
160         outer.add(buttons);\r
161         outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
162         outer.addStyleName("pithos-TabPanelBottom");\r
163 \r
164         focusPanel.setFocus(true);\r
165         setWidget(outer);\r
166         }\r
167 \r
168     private VerticalPanel createGeneralPanel() {\r
169         final VerticalPanel generalPanel = new VerticalPanel();\r
170         final FlexTable generalTable = new FlexTable();\r
171         generalTable.setText(0, 0, "Name");\r
172         generalTable.setText(1, 0, "Folder");\r
173         generalTable.setText(2, 0, "Owner");\r
174         generalTable.setText(3, 0, "Last modified");\r
175         generalTable.setText(4, 0, "Tags");\r
176 \r
177         name.setWidth("100%");\r
178         name.setText(file.getName());\r
179         generalTable.setWidget(0, 1, name);\r
180         if(file.getParent() != null)\r
181             generalTable.setText(1, 1, file.getParent().getName());\r
182         else\r
183             generalTable.setText(1, 1, "-");\r
184         generalTable.setText(2, 1, file.getOwner());\r
185 \r
186         final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");\r
187         generalTable.setText(3, 1, formatter.format(file.getLastModified()));\r
188 \r
189                 StringBuffer tagsBuffer = new StringBuffer();\r
190         for (String t : file.getTags())\r
191                         tagsBuffer.append(t).append(", ");\r
192                 if (tagsBuffer.length() > 1)\r
193                         tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);\r
194                 initialTagText = tagsBuffer.toString();\r
195                 tags.setWidth("100%");\r
196                 tags.setText(initialTagText);\r
197                 generalTable.setWidget(4, 1, tags);\r
198 \r
199         generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");\r
200         generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");\r
201         generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");\r
202         generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");\r
203         generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");\r
204         generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");\r
205         generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");\r
206         generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");\r
207         generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");\r
208         generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");\r
209         generalTable.setCellSpacing(4);\r
210 \r
211         generalPanel.add(generalTable);\r
212 \r
213         DisclosurePanel allTags = new DisclosurePanel("All tags");\r
214         allTagsContent = new FlowPanel();\r
215         allTagsContent.setWidth("100%");\r
216         for (Tag t : app.getAllTags()) {\r
217             final Anchor tagAnchor = new Anchor(t.getName(), false);\r
218             tagAnchor.addStyleName("pithos-tag");\r
219             allTagsContent.add(tagAnchor);\r
220             Label separator = new Label(", ");\r
221             separator.addStyleName("pithos-tag");\r
222             allTagsContent.add(separator);\r
223             tagAnchor.addClickHandler(new ClickHandler() {\r
224 \r
225                 @Override\r
226                 public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
227                     String existing = tags.getText().trim();\r
228                     if (MULTIPLE_VALUES_TEXT.equals(existing))\r
229                         existing = "";\r
230                     String newTag = tagAnchor.getText().trim();\r
231                     // insert the new tag only if it is not in the list\r
232                     // already\r
233                     if (existing.indexOf(newTag) == -1)\r
234                         tags.setText(existing + (existing.length() > 0 ? ", " : "") + newTag);\r
235                 }\r
236             });\r
237         }\r
238         allTags.setContent(allTagsContent);\r
239         generalPanel.add(allTags);\r
240         generalPanel.setSpacing(4);\r
241         return generalPanel;\r
242     }\r
243 \r
244     private VerticalPanel createSharingPanel() {\r
245         VerticalPanel permPanel = new VerticalPanel();\r
246 \r
247         permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);\r
248         permPanel.add(permList);\r
249 \r
250         if (file.getInheritedPermissionsFrom() == null) {\r
251             HorizontalPanel permButtons = new HorizontalPanel();\r
252             Button add = new Button("Add Group", new ClickHandler() {\r
253                 @Override\r
254                 public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
255                     PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);\r
256                     dlg.center();\r
257                     permList.updatePermissionTable();\r
258                 }\r
259             });\r
260             permButtons.add(add);\r
261             permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);\r
262 \r
263             final Button addUser = new Button("Add User", new ClickHandler() {\r
264                 @Override\r
265                 public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
266                     PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);\r
267                     dlg.center();\r
268                     permList.updatePermissionTable();\r
269                 }\r
270             });\r
271             permButtons.add(addUser);\r
272             permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);\r
273 \r
274             permButtons.setSpacing(8);\r
275             permButtons.addStyleName("pithos-TabPanelBottom");\r
276             permPanel.add(permButtons);\r
277         }\r
278 \r
279         final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +\r
280                     " by everyone. By checking this option, you are certifying that you have the right to " +\r
281                     "distribute this file and that it does not violate the Terms of Use.", true);\r
282         readForAllNote.setVisible(false);\r
283         readForAllNote.setStylePrimaryName("pithos-readForAllNote");\r
284 \r
285         readForAll = new CheckBox();\r
286         readForAll.setValue(file.isPublished());\r
287         readForAll.addClickHandler(new ClickHandler() {\r
288             @Override\r
289             public void onClick(@SuppressWarnings("unused") ClickEvent event) {\r
290                 readForAllNote.setVisible(readForAll.getValue());\r
291             }\r
292         });\r
293 \r
294         // Only show the read for all permission if the user is the owner.\r
295         if (file.getOwner().equals(app.getUsername())) {\r
296             final HorizontalPanel permForAll = new HorizontalPanel();\r
297             permForAll.add(new Label("Public"));\r
298             permForAll.add(readForAll);\r
299             permForAll.setSpacing(8);\r
300             permForAll.addStyleName("pithos-TabPanelBottom");\r
301             permForAll.add(readForAllNote);\r
302             permPanel.add(permForAll);\r
303         }\r
304 \r
305         if (file.isPublished()) {\r
306             final HorizontalPanel pathPanel = new HorizontalPanel();\r
307             pathPanel.setWidth("100%");\r
308             pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);\r
309             pathPanel.add(new Label("Link"));\r
310             pathPanel.setSpacing(8);\r
311             pathPanel.addStyleName("pithos-TabPanelBottom");\r
312 \r
313             TextBox path = new TextBox();\r
314             path.setWidth("100%");\r
315             path.addClickHandler(new ClickHandler() {\r
316                 @Override\r
317                 public void onClick(ClickEvent event) {\r
318                     Pithos.enableIESelection();\r
319                     ((TextBox) event.getSource()).selectAll();\r
320                     Pithos.preventIESelection();\r
321                 }\r
322             });\r
323             path.setText(Window.Location.getHost() + file.getPublicUri());\r
324             path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");\r
325             path.setWidth("100%");\r
326             path.setReadOnly(true);\r
327             pathPanel.add(path);\r
328             permPanel.add(pathPanel);\r
329         }\r
330 \r
331         return permPanel;\r
332     }\r
333 \r
334     private VerticalPanel createVersionPanel() {\r
335         VerticalPanel versionPanel = new VerticalPanel();\r
336 \r
337 //        VersionsList verList = new VersionsList(this, images, bodies);\r
338 //        versionPanel.add(verList);\r
339 //\r
340 //        HorizontalPanel vPanel = new HorizontalPanel();\r
341 //\r
342 //              vPanel.setSpacing(8);\r
343 //              vPanel.addStyleName("pithos-TabPanelBottom");\r
344 //              vPanel.add(new Label("Versioned"));\r
345 //\r
346 //              versioned.setValue(file.isVersioned());\r
347 //              vPanel.add(versioned);\r
348 //              versionPanel.add(vPanel);\r
349 //\r
350 //        HorizontalPanel vPanel2 = new HorizontalPanel();\r
351 //              vPanel2.setSpacing(8);\r
352 //              vPanel2.addStyleName("pithos-TabPanelBottom");\r
353 //\r
354 //        HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");\r
355 //        vPanel2.add(removeAllVersion);\r
356 //\r
357 //              Button removeVersionsButton = new Button(AbstractImagePrototype.create(images.delete()).getHTML(), new ClickHandler() {\r
358 //                      @Override\r
359 //                      public void onClick(ClickEvent event) {\r
360 //                              ConfirmationDialog confirm = new ConfirmationDialog("Really " +\r
361 //                                              "remove all previous versions?", "Remove") {\r
362 //\r
363 //                                      @Override\r
364 //                                      public void cancel() {\r
365 //                                      }\r
366 //\r
367 //                                      @Override\r
368 //                                      public void confirm() {\r
369 //                                              FilePropertiesDialog.this.closeDialog();\r
370 //                                              removeAllOldVersions();\r
371 //                                      }\r
372 //\r
373 //                              };\r
374 //                              confirm.center();\r
375 //                      }\r
376 //\r
377 //              });\r
378 //              vPanel2.add(removeVersionsButton);\r
379 //        if(!file.isVersioned())\r
380 //            vPanel2.setVisible(false);\r
381 //\r
382 //        versionPanel.add(vPanel2);\r
383 \r
384         return versionPanel;\r
385     }\r
386 \r
387         /**\r
388          * Accepts any change and updates the file\r
389          *\r
390          */\r
391         @Override\r
392         protected void accept() {\r
393                 String newFilename = null;\r
394 \r
395                 final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);\r
396 \r
397                 if (!name.getText().trim().equals(file.getName())) {\r
398                         newFilename = name.getText().trim();\r
399                 }\r
400 \r
401                 //only update the read for all perm if the user is the owner\r
402         Boolean published = null;\r
403                 if (readForAll.getValue() != file.isPublished())\r
404                         if (file.getOwner().equals(app.getUsername()))\r
405                 published = readForAll.getValue();\r
406         final Boolean finalPublished = published;\r
407 \r
408         String[] tagset = null;\r
409                 if (!tags.getText().equals(initialTagText))\r
410                         tagset = tags.getText().split(",");\r
411         final String[] newTags = tagset;\r
412 \r
413         if (newFilename != null) {\r
414             final String path = file.getParent().getUri() + "/" + newFilename;\r
415             PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {\r
416                 @Override\r
417                 public void onSuccess(@SuppressWarnings("unused") Resource result) {\r
418                     updateMetaData(app.getApiPath(), app.getUsername(), path + "?update=", newTags, finalPublished, perms);\r
419                 }\r
420 \r
421                 @Override\r
422                 public void onError(Throwable t) {\r
423                     GWT.log("", t);\r
424                     app.displayError("System error modifying file:" + t.getMessage());\r
425                 }\r
426             };\r
427             updateFile.setHeader("X-Auth-Token", app.getToken());\r
428             updateFile.setHeader("X-Move-From", file.getUri());\r
429             updateFile.setHeader("Content-Type", file.getContentType());\r
430             Scheduler.get().scheduleDeferred(updateFile);\r
431         }\r
432         else\r
433             updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished, perms);\r
434         }\r
435 \r
436         protected void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published, Map<String, Boolean[]> newPermissions) {\r
437         if (newTags != null || published != null || newPermissions != null) {\r
438             PostRequest updateFile = new PostRequest(api, owner, path) {\r
439                 @Override\r
440                 public void onSuccess(@SuppressWarnings("unused") Resource result) {\r
441                     app.updateFolder(file.getParent(), true, null);\r
442                 }\r
443 \r
444                 @Override\r
445                 public void onError(Throwable t) {\r
446                     GWT.log("", t);\r
447                     app.displayError("System error modifying file:" + t.getMessage());\r
448                 }\r
449             };\r
450             updateFile.setHeader("X-Auth-Token", app.getToken());\r
451             if (newTags != null)\r
452                 for (String t : newTags)\r
453                     updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");\r
454             if (published != null)\r
455                 updateFile.setHeader("X-Object-Public", published.toString());\r
456             if (newPermissions != null) {\r
457                 String readPermHeader = "read=";\r
458                 String writePermHeader = "write=";\r
459                 for (String u : newPermissions.keySet()) {\r
460                     Boolean[] p = newPermissions.get(u);\r
461                     if (p[0] != null && p[0])\r
462                         readPermHeader += u + ",";\r
463                     if (p[1] != null && p[1])\r
464                         writePermHeader += u + ",";\r
465                 }\r
466                 if (readPermHeader.endsWith("="))\r
467                     readPermHeader = "";\r
468                 else if (readPermHeader.endsWith(","))\r
469                     readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);\r
470                 if (writePermHeader.endsWith("="))\r
471                     writePermHeader = "";\r
472                 else if (writePermHeader.endsWith(","))\r
473                     writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);\r
474                 String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;\r
475                 if (permHeader.length() == 0)\r
476                     permHeader="~";\r
477                 updateFile.setHeader("X-Object-Sharing", permHeader);\r
478             }\r
479             Scheduler.get().scheduleDeferred(updateFile);\r
480         }\r
481         else\r
482             app.updateFolder(file.getParent(), true, null);\r
483     }\r
484 \r
485         private void removeAllOldVersions() {\r
486         }\r
487 }\r