Added ids in homefolder(tree.homeFolder), Trash(tree.Trash), My Shared (tree.myShared...
[pithos] / src / gr / ebs / gss / client / FilePropertiesDialog.java
1 /*\r
2  * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import gr.ebs.gss.client.rest.PostCommand;\r
22 import gr.ebs.gss.client.rest.RestException;\r
23 import gr.ebs.gss.client.rest.resource.FileResource;\r
24 import gr.ebs.gss.client.rest.resource.GroupResource;\r
25 import gr.ebs.gss.client.rest.resource.PermissionHolder;\r
26 \r
27 import java.util.Iterator;\r
28 import java.util.List;\r
29 import java.util.Set;\r
30 \r
31 import com.google.gwt.core.client.GWT;\r
32 import com.google.gwt.event.dom.client.ChangeEvent;\r
33 import com.google.gwt.event.dom.client.ChangeHandler;\r
34 import com.google.gwt.event.dom.client.ClickEvent;\r
35 import com.google.gwt.event.dom.client.ClickHandler;\r
36 import com.google.gwt.i18n.client.DateTimeFormat;\r
37 import com.google.gwt.json.client.JSONArray;\r
38 import com.google.gwt.json.client.JSONBoolean;\r
39 import com.google.gwt.json.client.JSONObject;\r
40 import com.google.gwt.json.client.JSONString;\r
41 import com.google.gwt.resources.client.ClientBundle;\r
42 import com.google.gwt.resources.client.ImageResource;\r
43 import com.google.gwt.user.client.Command;\r
44 import com.google.gwt.user.client.DeferredCommand;\r
45 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
46 import com.google.gwt.user.client.ui.Button;\r
47 import com.google.gwt.user.client.ui.CheckBox;\r
48 import com.google.gwt.user.client.ui.DecoratedTabPanel;\r
49 import com.google.gwt.user.client.ui.DisclosurePanel;\r
50 import com.google.gwt.user.client.ui.FlexTable;\r
51 import com.google.gwt.user.client.ui.FlowPanel;\r
52 import com.google.gwt.user.client.ui.FocusPanel;\r
53 import com.google.gwt.user.client.ui.HTML;\r
54 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
55 import com.google.gwt.user.client.ui.HorizontalPanel;\r
56 import com.google.gwt.user.client.ui.Label;\r
57 import com.google.gwt.user.client.ui.TextBox;\r
58 import com.google.gwt.user.client.ui.VerticalPanel;\r
59 \r
60 /**\r
61  * The 'File properties' dialog box implementation.\r
62  *\r
63  * @author past\r
64  */\r
65 public class FilePropertiesDialog extends AbstractPropertiesDialog {\r
66 \r
67         final PermissionsList permList;\r
68 \r
69         private CheckBox readForAll;\r
70 \r
71         /**\r
72          * An image bundle for this widgets images.\r
73          */\r
74         public interface Images extends ClientBundle,MessagePanel.Images {\r
75 \r
76                 @Source("gr/ebs/gss/resources/edit_user.png")\r
77                 ImageResource permUser();\r
78 \r
79                 @Source("gr/ebs/gss/resources/groupevent.png")\r
80                 ImageResource permGroup();\r
81 \r
82                 @Source("gr/ebs/gss/resources/editdelete.png")\r
83                 ImageResource delete();\r
84 \r
85                 @Source("gr/ebs/gss/resources/db_update.png")\r
86                 ImageResource restore();\r
87 \r
88                 @Source("gr/ebs/gss/resources/folder_inbox.png")\r
89                 ImageResource download();\r
90         }\r
91 \r
92         /**\r
93          * The widget that holds the name of the file.\r
94          */\r
95         private TextBox name = new TextBox();\r
96 \r
97         private final CheckBox versioned = new CheckBox();\r
98 \r
99         final FileResource file;\r
100 \r
101         private String userFullName;\r
102 \r
103         /**\r
104          * The widget's constructor.\r
105          *\r
106          * @param images the dialog's ImageBundle\r
107          * @param groups\r
108          * @param bodies\r
109          */\r
110         public FilePropertiesDialog(final Images images, final List<GroupResource> groups, List<FileResource> bodies, String _userFullName) {\r
111 \r
112                 // Set the dialog's caption.\r
113                 setText("File properties");\r
114 \r
115                 file = (FileResource) GSS.get().getCurrentSelection();\r
116                 userFullName = _userFullName;\r
117                 permList = new PermissionsList(images, file.getPermissions(), file.getOwner());\r
118 \r
119 \r
120                 // Outer contains inner and buttons.\r
121                 final VerticalPanel outer = new VerticalPanel();\r
122                 final FocusPanel focusPanel = new FocusPanel(outer);\r
123                 // Inner contains generalPanel and permPanel.\r
124                 inner = new DecoratedTabPanel();\r
125                 inner.setAnimationEnabled(true);\r
126                 final VerticalPanel generalPanel = new VerticalPanel();\r
127                 final VerticalPanel permPanel = new VerticalPanel();\r
128                 final HorizontalPanel buttons = new HorizontalPanel();\r
129                 final HorizontalPanel permButtons = new HorizontalPanel();\r
130                 final HorizontalPanel permForAll = new HorizontalPanel();\r
131                 final HorizontalPanel pathPanel = new HorizontalPanel();\r
132                 final VerticalPanel verPanel = new VerticalPanel();\r
133                 final HorizontalPanel vPanel = new HorizontalPanel();\r
134                 final HorizontalPanel vPanel2 = new HorizontalPanel();\r
135 \r
136                 versioned.setValue(file.isVersioned());\r
137                 inner.add(generalPanel, "General");\r
138                 inner.add(permPanel, "Sharing");\r
139                 inner.add(verPanel, "Versions");\r
140                 inner.selectTab(0);\r
141 \r
142                 final Label fileNameNote = new Label("Please note that slashes ('/') are not allowed in file names.", true);\r
143                 fileNameNote.setVisible(false);\r
144                 fileNameNote.setStylePrimaryName("gss-readForAllNote");\r
145 \r
146                 final FlexTable generalTable = new FlexTable();\r
147                 generalTable.setText(0, 0, "Name");\r
148                 generalTable.setText(1, 0, "Folder");\r
149                 generalTable.setText(2, 0, "Owner");\r
150                 generalTable.setText(3, 0, "Last modified");\r
151                 generalTable.setText(4, 0, "Tags");\r
152                 name.setWidth("100%");\r
153                 name.setText(file.getName());\r
154                 generalTable.setWidget(0, 1, name);\r
155                 name.addChangeHandler(new ChangeHandler() {\r
156 \r
157                         @Override\r
158                         public void onChange(ChangeEvent event) {\r
159                                 if(name.getText().contains("/"))\r
160                                         fileNameNote.setVisible(true);\r
161                                 else\r
162                                         fileNameNote.setVisible(false);\r
163 \r
164                         }\r
165                 });\r
166 \r
167                 if(file.getFolderName() != null)\r
168                         generalTable.setText(1, 1, file.getFolderName());\r
169                 else\r
170                         generalTable.setText(1, 1, "-");\r
171                 generalTable.setWidget(0, 2, fileNameNote);\r
172                 generalTable.setText(2, 1,userFullName);\r
173 \r
174                 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");\r
175                 generalTable.setText(3, 1, formatter.format(file.getModificationDate()));\r
176                 // Get the tags.\r
177                 StringBuffer tagsBuffer = new StringBuffer();\r
178                 Iterator i = file.getTags().iterator();\r
179                 while (i.hasNext()) {\r
180                         String tag = (String) i.next();\r
181                         tagsBuffer.append(tag).append(", ");\r
182                 }\r
183                 if (tagsBuffer.length() > 1)\r
184                         tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);\r
185                 initialTagText = tagsBuffer.toString();\r
186                 tags.setWidth("100%");\r
187                 tags.setText(initialTagText);\r
188                 generalTable.setWidget(4, 1, tags);\r
189                 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");\r
190                 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");\r
191                 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");\r
192                 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");\r
193                 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");\r
194                 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");\r
195                 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");\r
196                 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");\r
197                 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");\r
198                 generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");\r
199                 generalTable.setCellSpacing(4);\r
200 \r
201                 // Create the 'OK' button, along with a listener that hides the dialog\r
202                 // when the button is clicked.\r
203                 final Button ok = new Button("OK", new ClickHandler() {\r
204                         @Override\r
205                         public void onClick(ClickEvent event) {\r
206                                 if(name.getText().contains("/"))\r
207                                         fileNameNote.setVisible(true);\r
208                                 else{\r
209                                         fileNameNote.setVisible(true);\r
210                                         accept();\r
211                                         closeDialog();\r
212                                 }               \r
213                         }\r
214                 });\r
215                 buttons.add(ok);\r
216                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
217                 // Create the 'Cancel' button, along with a listener that hides the\r
218                 // dialog when the button is clicked.\r
219                 final Button cancel = new Button("Cancel", new ClickHandler() {\r
220                         @Override\r
221                         public void onClick(ClickEvent event) {\r
222                                 closeDialog();\r
223                         }\r
224                 });\r
225                 buttons.add(cancel);\r
226                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
227                 buttons.setSpacing(8);\r
228                 buttons.addStyleName("gss-TabPanelBottom");\r
229 \r
230                 generalPanel.add(generalTable);\r
231 \r
232                 // Asynchronously retrieve the tags defined by this user.\r
233                 DeferredCommand.addCommand(new Command() {\r
234 \r
235                         @Override\r
236                         public void execute() {\r
237                                 updateTags();\r
238                         }\r
239                 });\r
240 \r
241                 DisclosurePanel allTags = new DisclosurePanel("All tags");\r
242                 allTagsContent = new FlowPanel();\r
243                 allTagsContent.setWidth("100%");\r
244                 allTags.setContent(allTagsContent);\r
245                 generalPanel.add(allTags);\r
246                 generalPanel.setSpacing(4);\r
247 \r
248                 final Button add = new Button("Add Group", new ClickHandler() {\r
249                         @Override\r
250                         public void onClick(ClickEvent event) {\r
251                                 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);\r
252                                 dlg.center();\r
253                         }\r
254                 });\r
255                 permButtons.add(add);\r
256                 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);\r
257 \r
258                 final Button addUser = new Button("Add User", new ClickHandler() {\r
259                         @Override\r
260                         public void onClick(ClickEvent event) {\r
261                                 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);\r
262                                 dlg.center();\r
263                         }\r
264                 });\r
265                 permButtons.add(addUser);\r
266                 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);\r
267 \r
268                 permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
269                 permButtons.setSpacing(8);\r
270                 permButtons.addStyleName("gss-TabPanelBottom");\r
271 \r
272                 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +\r
273                                         " by everyone. By checking this option, you are certifying that you have the right to " +\r
274                                         "distribute this file and that it does not violate the Terms of Use.", true);\r
275                 readForAllNote.setVisible(false);\r
276                 readForAllNote.setStylePrimaryName("gss-readForAllNote");\r
277 \r
278                 readForAll = new CheckBox();\r
279                 readForAll.setValue(file.isReadForAll());\r
280                 readForAll.addClickHandler(new ClickHandler() {\r
281                         @Override\r
282                         public void onClick(ClickEvent event) {\r
283                                 readForAllNote.setVisible(readForAll.getValue());\r
284                         }\r
285 \r
286                 });\r
287 \r
288                 permPanel.add(permList);\r
289                 permPanel.add(permButtons);\r
290                 // Only show the read for all permission if the user is the owner.\r
291                 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {\r
292                         permForAll.add(new Label("Public"));\r
293                         permForAll.add(readForAll);\r
294                         permForAll.setSpacing(8);\r
295                         permForAll.addStyleName("gss-TabPanelBottom");\r
296                         permForAll.add(readForAllNote);\r
297                         permPanel.add(permForAll);\r
298                 }\r
299 \r
300                 TextBox path = new TextBox();\r
301                 path.setWidth("100%");\r
302                 path.addClickHandler(new ClickHandler() {\r
303                         @Override\r
304                         public void onClick(ClickEvent event) {\r
305                                 GSS.enableIESelection();\r
306                                 ((TextBox) event.getSource()).selectAll();\r
307                                 GSS.preventIESelection();\r
308                         }\r
309 \r
310                 });\r
311                 path.setText(file.getUri());\r
312                 path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");\r
313                 path.setWidth("100%");\r
314                 path.setReadOnly(true);\r
315                 pathPanel.setWidth("100%");\r
316                 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);\r
317                 pathPanel.add(new Label("Link"));\r
318                 pathPanel.setSpacing(8);\r
319                 pathPanel.addStyleName("gss-TabPanelBottom");\r
320                 pathPanel.add(path);\r
321                 permPanel.add(pathPanel);\r
322 \r
323                 VersionsList verList = new VersionsList(this, images, bodies);\r
324                 verPanel.add(verList);\r
325 \r
326                 vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
327                 vPanel.setSpacing(8);\r
328                 vPanel.addStyleName("gss-TabPanelBottom");\r
329                 vPanel.add(new Label("Versioned"));\r
330 \r
331                 vPanel.add(versioned);\r
332                 verPanel.add(vPanel);\r
333                 vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
334                 vPanel2.setSpacing(8);\r
335                 vPanel2.addStyleName("gss-TabPanelBottom");\r
336                 Button removeVersionsButton = new Button(AbstractImagePrototype.create(images.delete()).getHTML(), new ClickHandler() {\r
337                         @Override\r
338                         public void onClick(ClickEvent event) {\r
339                                 ConfirmationDialog confirm = new ConfirmationDialog("Really " +\r
340                                                 "remove all previous versions?", "Remove") {\r
341 \r
342                                         @Override\r
343                                         public void cancel() {\r
344                                         }\r
345 \r
346                                         @Override\r
347                                         public void confirm() {\r
348                                                 FilePropertiesDialog.this.closeDialog();\r
349                                                 removeAllOldVersions();\r
350                                         }\r
351 \r
352                                 };\r
353                                 confirm.center();\r
354                         }\r
355 \r
356                 });\r
357                 HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");\r
358                 vPanel2.add(removeAllVersion);\r
359                 vPanel2.add(removeVersionsButton);\r
360                 verPanel.add(vPanel2);\r
361                 if(!file.isVersioned())\r
362                         vPanel2.setVisible(false);\r
363                 outer.add(inner);\r
364                 outer.add(buttons);\r
365                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
366                 outer.addStyleName("gss-TabPanelBottom");\r
367 \r
368                 focusPanel.setFocus(true);\r
369                 setWidget(outer);\r
370         }\r
371 \r
372 \r
373         /**\r
374          * Accepts any change and updates the file\r
375          *\r
376          */\r
377         @Override\r
378         protected void accept() {\r
379                 String newFilename = null;\r
380                 permList.updatePermissionsAccordingToInput();\r
381                 Set<PermissionHolder> perms = permList.getPermissions();\r
382                 JSONObject json = new JSONObject();\r
383                 if (!name.getText().equals(file.getName())) {\r
384                         newFilename = name.getText();\r
385                         json.put("name", new JSONString(newFilename));\r
386                 }\r
387                 if (versioned.getValue() != file.isVersioned())\r
388                         json.put("versioned", JSONBoolean.getInstance(versioned.getValue()));\r
389                 //only update the read for all perm if the user is the owner\r
390                 if (readForAll.getValue() != file.isReadForAll())\r
391                         if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))\r
392                                 json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));\r
393                 int i = 0;\r
394                 if (permList.hasChanges()) {\r
395                         GWT.log("Permissions change", null);\r
396                         JSONArray perma = new JSONArray();\r
397 \r
398                         for (PermissionHolder p : perms) {\r
399                                 JSONObject po = new JSONObject();\r
400                                 if (p.getUser() != null)\r
401                                         po.put("user", new JSONString(p.getUser()));\r
402                                 if (p.getGroup() != null)\r
403                                         po.put("group", new JSONString(p.getGroup()));\r
404                                 po.put("read", JSONBoolean.getInstance(p.isRead()));\r
405                                 po.put("write", JSONBoolean.getInstance(p.isWrite()));\r
406                                 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));\r
407                                 perma.set(i, po);\r
408                                 i++;\r
409                         }\r
410                         json.put("permissions", perma);\r
411                 }\r
412                 JSONArray taga = new JSONArray();\r
413                 i = 0;\r
414                 if (!tags.getText().equals(initialTagText)) {\r
415                         String[] tagset = tags.getText().split(",");\r
416                         for (String t : tagset) {\r
417                                 JSONString to = new JSONString(t);\r
418                                 taga.set(i, to);\r
419                                 i++;\r
420                         }\r
421                         json.put("tags", taga);\r
422                 }\r
423                 String jsonString = json.toString();\r
424                 if(jsonString.equals("{}")){\r
425                         GWT.log("NO CHANGES", null);\r
426                         return;\r
427                 }\r
428                 final String newFilenameFinal = newFilename;\r
429                 PostCommand cf = new PostCommand(file.getUri() + "?update=", jsonString, 200) {\r
430 \r
431                         @Override\r
432                         public void onComplete() {\r
433                                 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/, newFilenameFinal);\r
434                         }\r
435 \r
436                         @Override\r
437                         public void onError(Throwable t) {\r
438                                 GWT.log("", t);\r
439                                 if (t instanceof RestException) {\r
440                                         int statusCode = ((RestException) t).getHttpStatusCode();\r
441                                         if (statusCode == 405)\r
442                                                 GSS.get().displayError("You don't have the necessary permissions");\r
443                                         else if (statusCode == 404)\r
444                                                 GSS.get().displayError("User in permissions does not exist");\r
445                                         else if (statusCode == 409)\r
446                                                 GSS.get().displayError("A file with the same name already exists");\r
447                                         else if (statusCode == 413)\r
448                                                 GSS.get().displayError("Your quota has been exceeded");\r
449                                         else\r
450                                                 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
451                                 } else\r
452                                         GSS.get().displayError("System error modifying file:" + t.getMessage());\r
453                         }\r
454 \r
455                 };\r
456                 DeferredCommand.addCommand(cf);\r
457 \r
458         }\r
459 \r
460         private void removeAllOldVersions() {\r
461                 JSONObject json = new JSONObject();\r
462                 json.put("versioned", JSONBoolean.getInstance(false));\r
463                 GWT.log(json.toString(), null);\r
464                 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {\r
465 \r
466                         @Override\r
467                         public void onComplete() {\r
468                                 toggleVersioned(true);\r
469                         }\r
470 \r
471                         @Override\r
472                         public void onError(Throwable t) {\r
473                                 GWT.log("", t);\r
474                                 if (t instanceof RestException) {\r
475                                         int statusCode = ((RestException) t).getHttpStatusCode();\r
476                                         if (statusCode == 405)\r
477                                                 GSS.get().displayError("You don't have the necessary permissions");\r
478                                         else if (statusCode == 404)\r
479                                                 GSS.get().displayError("User in permissions does not exist");\r
480                                         else if (statusCode == 409)\r
481                                                 GSS.get().displayError("A folder with the same name already exists");\r
482                                         else if (statusCode == 413)\r
483                                                 GSS.get().displayError("Your quota has been exceeded");\r
484                                         else\r
485                                                 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
486                                 } else\r
487                                         GSS.get().displayError("System error moifying file:" + t.getMessage());\r
488                         }\r
489                 };\r
490                 DeferredCommand.addCommand(cf);\r
491         }\r
492 \r
493         private void toggleVersioned(boolean versionedValue) {\r
494                 JSONObject json = new JSONObject();\r
495                 json.put("versioned", JSONBoolean.getInstance(versionedValue));\r
496                 GWT.log(json.toString(), null);\r
497                 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {\r
498 \r
499                         @Override\r
500                         public void onComplete() {\r
501                                 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);\r
502                         }\r
503 \r
504                         @Override\r
505                         public void onError(Throwable t) {\r
506                                 GWT.log("", t);\r
507                                 if (t instanceof RestException) {\r
508                                         int statusCode = ((RestException) t).getHttpStatusCode();\r
509                                         if (statusCode == 405)\r
510                                                 GSS.get().displayError("You don't have the necessary permissions");\r
511                                         else if (statusCode == 404)\r
512                                                 GSS.get().displayError("User in permissions does not exist");\r
513                                         else if (statusCode == 409)\r
514                                                 GSS.get().displayError("A folder with the same name already exists");\r
515                                         else if (statusCode == 413)\r
516                                                 GSS.get().displayError("Your quota has been exceeded");\r
517                                         else\r
518                                                 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
519                                 } else\r
520                                         GSS.get().displayError("System error moifying file:" + t.getMessage());\r
521                         }\r
522                 };\r
523                 DeferredCommand.addCommand(cf);\r
524         }\r
525 \r
526 }\r