Fixed username in all API calls + some other fixes
[pithos-web-client] / src / gr / grnet / pithos / web / client / Pithos.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client;
36
37 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
38 import gr.grnet.pithos.web.client.foldertree.AccountResource;
39 import gr.grnet.pithos.web.client.foldertree.File;
40 import gr.grnet.pithos.web.client.foldertree.Folder;
41 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
42 import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
43 import gr.grnet.pithos.web.client.foldertree.Resource;
44 import gr.grnet.pithos.web.client.grouptree.Group;
45 import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
46 import gr.grnet.pithos.web.client.grouptree.GroupTreeViewModel;
47 import gr.grnet.pithos.web.client.grouptree.User;
48 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView;
49 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
50 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
51 import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
52 import gr.grnet.pithos.web.client.rest.DeleteRequest;
53 import gr.grnet.pithos.web.client.rest.GetRequest;
54 import gr.grnet.pithos.web.client.rest.PutRequest;
55 import gr.grnet.pithos.web.client.rest.RestException;
56 import gr.grnet.pithos.web.client.tagtree.Tag;
57 import gr.grnet.pithos.web.client.tagtree.TagTreeView;
58 import gr.grnet.pithos.web.client.tagtree.TagTreeViewModel;
59
60 import java.util.ArrayList;
61 import java.util.HashMap;
62 import java.util.Iterator;
63 import java.util.List;
64 import java.util.Set;
65
66 import com.google.gwt.core.client.EntryPoint;
67 import com.google.gwt.core.client.GWT;
68 import com.google.gwt.core.client.Scheduler;
69 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
70 import com.google.gwt.event.dom.client.ClickEvent;
71 import com.google.gwt.event.dom.client.ClickHandler;
72 import com.google.gwt.event.logical.shared.ResizeEvent;
73 import com.google.gwt.event.logical.shared.ResizeHandler;
74 import com.google.gwt.http.client.Request;
75 import com.google.gwt.http.client.RequestBuilder;
76 import com.google.gwt.http.client.RequestCallback;
77 import com.google.gwt.http.client.RequestException;
78 import com.google.gwt.http.client.Response;
79 import com.google.gwt.json.client.JSONArray;
80 import com.google.gwt.json.client.JSONObject;
81 import com.google.gwt.json.client.JSONParser;
82 import com.google.gwt.json.client.JSONString;
83 import com.google.gwt.json.client.JSONValue;
84 import com.google.gwt.resources.client.ImageResource;
85 import com.google.gwt.user.client.Command;
86 import com.google.gwt.user.client.Cookies;
87 import com.google.gwt.user.client.Event;
88 import com.google.gwt.user.client.History;
89 import com.google.gwt.user.client.Window;
90 import com.google.gwt.user.client.ui.AbstractImagePrototype;
91 import com.google.gwt.user.client.ui.Button;
92 import com.google.gwt.user.client.ui.HTML;
93 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
94 import com.google.gwt.user.client.ui.HasVerticalAlignment;
95 import com.google.gwt.user.client.ui.HorizontalPanel;
96 import com.google.gwt.user.client.ui.HorizontalSplitPanel;
97 import com.google.gwt.user.client.ui.Image;
98 import com.google.gwt.user.client.ui.PushButton;
99 import com.google.gwt.user.client.ui.RootPanel;
100 import com.google.gwt.user.client.ui.VerticalPanel;
101 import com.google.gwt.view.client.SelectionChangeEvent;
102 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
103 import com.google.gwt.view.client.SingleSelectionModel;
104
105 /**
106  * Entry point classes define <code>onModuleLoad()</code>.
107  */
108 public class Pithos implements EntryPoint, ResizeHandler {
109
110         public static final String HOME_CONTAINER = "pithos";
111
112         public static final String TRASH_CONTAINER = "trash";
113         
114         /**
115          * Instantiate an application-level image bundle. This object will provide
116          * programmatic access to all the images needed by widgets.
117          */
118         private static Images images = (Images) GWT.create(Images.class);
119
120     public String getUsername() {
121         return username;
122     }
123
124     public void setAccount(AccountResource acct) {
125         account = acct;
126     }
127
128     public AccountResource getAccount() {
129         return account;
130     }
131
132     public void updateFolder(Folder f, boolean showfiles, Command callback) {
133         folderTreeView.updateFolder(f, showfiles, callback);
134     }
135
136     public void updateGroupNode(Group group) {
137         groupTreeView.updateGroupNode(group);
138     }
139
140     public void updateSharedFolder(Folder f, boolean showfiles) {
141         mysharedTreeView.updateFolder(f, showfiles);
142     }
143     
144     public void updateOtherSharedFolder(Folder f, boolean showfiles) {
145         otherSharedTreeView.updateFolder(f, showfiles);
146     }
147
148     public void updateTag(Tag t) {
149         tagTreeView.updateTag(t);
150     }
151
152     public void updateTags() {
153         tagTreeViewModel.initialize(getAllTags());
154     }
155
156     public List<Tag> getAllTags() {
157         List<Tag> tagList = new ArrayList<Tag>();
158         for (Folder f : account.getContainers()) {
159             for (String t : f.getTags()) {
160                 tagList.add(new Tag(t));
161             }
162         }
163         return tagList;
164     }
165
166     public MysharedTreeView getMySharedTreeView() {
167         return mysharedTreeView;
168     }
169
170     /**
171          * An aggregate image bundle that pulls together all the images for this
172          * application into a single bundle.
173          */
174         public interface Images extends TopPanel.Images, FileList.Images {
175
176                 @Source("gr/grnet/pithos/resources/document.png")
177                 ImageResource folders();
178
179                 @Source("gr/grnet/pithos/resources/edit_group_22.png")
180                 ImageResource groups();
181
182                 @Source("gr/grnet/pithos/resources/search.png")
183                 ImageResource search();
184         }
185
186         /**
187          * The Application Clipboard implementation;
188          */
189         private Clipboard clipboard = new Clipboard();
190
191         /**
192          * The top panel that contains the menu bar.
193          */
194         private TopPanel topPanel;
195
196         /**
197          * The panel that contains the various system messages.
198          */
199         private MessagePanel messagePanel = new MessagePanel(Pithos.images);
200
201         /**
202          * The bottom panel that contains the status bar.
203          */
204         private StatusPanel statusPanel = null;
205
206         /**
207          * The file list widget.
208          */
209         private FileList fileList;
210
211         /**
212          * The tab panel that occupies the right side of the screen.
213          */
214         private VerticalPanel inner = new VerticalPanel();
215
216
217         /**
218          * The split panel that will contain the left and right panels.
219          */
220         private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
221
222         /**
223          * The currently selected item in the application, for use by the Edit menu
224          * commands. Potential types are Folder, File, User and Group.
225          */
226         private Object currentSelection;
227
228
229         /**
230          * The WebDAV password of the current user
231          */
232         private String webDAVPassword;
233
234         public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
235
236     private String username = null;
237
238     /**
239      * The authentication token of the current user.
240      */
241     private String token;
242
243     SingleSelectionModel<Folder> folderTreeSelectionModel;
244     FolderTreeViewModel folderTreeViewModel;
245     FolderTreeView folderTreeView;
246
247     SingleSelectionModel<Folder> mysharedTreeSelectionModel;
248     private MysharedTreeViewModel mysharedTreeViewModel;
249     MysharedTreeView mysharedTreeView;
250
251     protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
252     private OtherSharedTreeViewModel otherSharedTreeViewModel;
253     OtherSharedTreeView otherSharedTreeView;
254
255     protected SingleSelectionModel<Tag> tagTreeSelectionModel;
256     private TagTreeViewModel tagTreeViewModel;
257     private TagTreeView tagTreeView;
258
259     private GroupTreeViewModel groupTreeViewModel;
260     private GroupTreeView groupTreeView;
261
262     private TreeView selectedTree;
263     protected AccountResource account;
264     
265     private Folder trash;
266
267     @SuppressWarnings("rawtypes")
268         private List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
269     
270     Button upload;
271     
272     private HTML totalFiles;
273     
274     private HTML usedBytes;
275     
276     private HTML totalBytes;
277     
278     private HTML usedPercent;
279
280         @Override
281         public void onModuleLoad() {
282                 if (parseUserCredentials())
283             initialize();
284         }
285
286     private void initialize() {
287         VerticalPanel outer = new VerticalPanel();
288         outer.setWidth("100%");
289
290         topPanel = new TopPanel(this, Pithos.images);
291         topPanel.setWidth("100%");
292         outer.add(topPanel);
293
294         messagePanel.setWidth("100%");
295         messagePanel.setVisible(false);
296         outer.add(messagePanel);
297         outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
298
299
300         // Inner contains the various lists.
301         inner.sinkEvents(Event.ONCONTEXTMENU);
302         inner.setWidth("100%");
303
304         HorizontalPanel rightside = new HorizontalPanel();
305         rightside.addStyleName("pithos-rightSide");
306         rightside.setSpacing(5);
307
308         HTML folderStatistics = new HTML("5 Files (size: 1.1GB)");
309         folderStatistics.addStyleName("pithos-folderStatistics");
310         rightside.add(folderStatistics);
311         inner.add(rightside);
312         inner.setCellHorizontalAlignment(rightside, HasHorizontalAlignment.ALIGN_RIGHT);
313         inner.setCellVerticalAlignment(rightside, HasVerticalAlignment.ALIGN_MIDDLE);
314         inner.setCellHeight(rightside, "60px");
315
316         folderTreeSelectionModel = new SingleSelectionModel<Folder>();
317         folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
318             @Override
319             public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
320                 if (folderTreeSelectionModel.getSelectedObject() != null) {
321                     deselectOthers(folderTreeView, folderTreeSelectionModel);
322                     applyPermissions(folderTreeSelectionModel.getSelectedObject());
323                     Folder f = folderTreeSelectionModel.getSelectedObject();
324                     updateFolder(f, true, null);
325                 }
326             }
327         });
328         selectionModels.add(folderTreeSelectionModel);
329
330         folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
331         folderTreeView = new FolderTreeView(folderTreeViewModel);
332
333         fileList = new FileList(this, images, folderTreeView);
334         inner.add(fileList);
335
336         mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
337         mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
338             @Override
339             public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
340                 if (mysharedTreeSelectionModel.getSelectedObject() != null) {
341                     deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
342                     upload.setEnabled(false);
343                     updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
344                 }
345             }
346         });
347         selectionModels.add(mysharedTreeSelectionModel);
348         mysharedTreeViewModel = new MysharedTreeViewModel(this, mysharedTreeSelectionModel);
349         mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
350
351         otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
352         otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
353             @Override
354             public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
355                 if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
356                     deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
357                     applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
358                     updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
359                 }
360             }
361         });
362         selectionModels.add(otherSharedTreeSelectionModel);
363         otherSharedTreeViewModel = new OtherSharedTreeViewModel(this, otherSharedTreeSelectionModel);
364         otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
365
366         groupTreeViewModel = new GroupTreeViewModel(this);
367         groupTreeView = new GroupTreeView(groupTreeViewModel);
368
369         VerticalPanel trees = new VerticalPanel();
370
371         upload = new Button("Upload File", new ClickHandler() {
372             @Override
373             public void onClick(@SuppressWarnings("unused") ClickEvent event) {
374                 new UploadFileCommand(Pithos.this, null, getSelection()).execute();
375             }
376         });
377         upload.addStyleName("pithos-uploadButton");
378         trees.add(upload);
379         
380         HorizontalPanel treeHeader = new HorizontalPanel();
381         treeHeader.addStyleName("pithos-treeHeader");
382         HorizontalPanel statistics = new HorizontalPanel();
383         statistics.add(new HTML("Total Files:&nbsp;"));
384         totalFiles = new HTML();
385         statistics.add(totalFiles);
386         statistics.add(new HTML("&nbsp;|&nbsp;Used:&nbsp;"));
387         usedBytes = new HTML();
388         statistics.add(usedBytes);
389         statistics.add(new HTML("&nbsp;of&nbsp;"));
390         totalBytes = new HTML();
391         statistics.add(totalBytes);
392         statistics.add(new HTML("&nbsp;("));
393         usedPercent = new HTML();
394         statistics.add(usedPercent);
395         statistics.add(new HTML("%)"));
396         treeHeader.add(statistics);
397         trees.add(treeHeader);
398
399         trees.add(folderTreeView);
400         trees.add(mysharedTreeView);
401         trees.add(otherSharedTreeView);
402 //        trees.add(tagTreeView);
403         trees.add(groupTreeView);
404         // Add the left and right panels to the split panel.
405         splitPanel.setLeftWidget(trees);
406         splitPanel.setRightWidget(inner);
407         splitPanel.setSplitPosition("25%");
408         splitPanel.setSize("100%", "100%");
409         splitPanel.addStyleName("pithos-splitPanel");
410         outer.add(splitPanel);
411
412         statusPanel = new StatusPanel();
413         outer.add(statusPanel);
414
415
416         // Hook the window resize event, so that we can adjust the UI.
417         Window.addResizeHandler(this);
418         // Clear out the window's built-in margin, because we want to take
419         // advantage of the entire client area.
420         Window.setMargin("0px");
421         // Finally, add the outer panel to the RootPanel, so that it will be
422         // displayed.
423         RootPanel.get().add(outer);
424         // Call the window resized handler to get the initial sizes setup. Doing
425         // this in a deferred command causes it to occur after all widgets'
426         // sizes have been computed by the browser.
427         Scheduler.get().scheduleDeferred(new ScheduledCommand() {
428
429             @Override
430             public void execute() {
431                 onWindowResized(Window.getClientHeight());
432             }
433         });
434
435         Scheduler.get().scheduleDeferred(new ScheduledCommand() {
436             @Override
437             public void execute() {
438                 fetchAccount();
439             }
440         });
441     }
442
443     public void applyPermissions(Folder f) {
444         if (f != null) {
445                 if (f.isInTrash())
446                         upload.setEnabled(false);
447                 else {
448                         Boolean[] perms = f.getPermissions().get(username);
449                         if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
450                                 upload.setEnabled(true);
451                         }
452                         else
453                                 upload.setEnabled(false);
454                 }
455         }
456         else
457                 upload.setEnabled(false);
458         }
459
460         @SuppressWarnings({ "rawtypes", "unchecked" })
461         public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
462         selectedTree = _selectedTree;
463         for (SingleSelectionModel s : selectionModels)
464             if (!s.equals(model))
465                 s.setSelected(s.getSelectedObject(), false);
466     }
467
468     public void showFiles(Folder f) {
469         Set<File> files = f.getFiles();
470         showFiles(files);
471     }
472
473     public void showFiles(Set<File> files) {
474         //Iterator<File> iter = files.iterator();
475         //fetchFile(iter, files);
476         fileList.setFiles(new ArrayList<File>(files));
477     }
478
479     protected void fetchFile(final Iterator<File> iter, final Set<File> files) {
480         if (iter.hasNext()) {
481             File file = iter.next();
482             String path = file.getUri() + "?format=json";
483             GetRequest<File> getFile = new GetRequest<File>(File.class, getApiPath(), username, path, file) {
484                 @Override
485                 public void onSuccess(@SuppressWarnings("unused") File _result) {
486                     fetchFile(iter, files);
487                 }
488
489                 @Override
490                 public void onError(Throwable t) {
491                     GWT.log("Error getting file", t);
492                     if (t instanceof RestException)
493                         displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
494                     else
495                         displayError("System error fetching file: " + t.getMessage());
496                 }
497             };
498             getFile.setHeader("X-Auth-Token", "0000");
499             Scheduler.get().scheduleDeferred(getFile);
500         }
501         else
502             fileList.setFiles(new ArrayList<File>(files));
503     }
504
505     /**
506          * Parse and store the user credentials to the appropriate fields.
507          */
508         private boolean parseUserCredentials() {
509         username = Window.Location.getParameter("user");
510         token = Window.Location.getParameter("token");
511         Configuration conf = (Configuration) GWT.create(Configuration.class);
512         if (username == null || username.length() == 0 || token == null || token.length() == 0) {
513             String cookie = conf.authCookie();
514             String auth = Cookies.getCookie(cookie);
515             if (auth == null) {
516                 authenticateUser();
517                 return false;
518             }
519                         String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
520                         if (authSplit.length != 2) {
521                             authenticateUser();
522                             return false;
523                         }
524                         username = authSplit[0];
525                         token = authSplit[1];
526                         return true;
527         }
528                 Cookies.setCookie(conf.authCookie(), username + conf.cookieSeparator() + token);
529                 return true;
530     }
531
532     /**
533          * Redirect the user to the login page for authentication.
534          */
535         protected void authenticateUser() {
536                 Configuration conf = (Configuration) GWT.create(Configuration.class);
537         Window.Location.assign(conf.loginUrl() + "?next=" + Window.Location.getHref());
538         }
539
540         protected void fetchAccount() {
541         String path = "?format=json";
542
543         GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
544             @Override
545             public void onSuccess(AccountResource _result) {
546                 account = _result;
547                 if (!account.hasHomeContainer())
548                     createHomeContainer(account);
549                 else if (!account.hasTrashContainer())
550                         createTrashContainer();
551                 else {
552                         for (Folder f : account.getContainers())
553                                 if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
554                                         trash = f;
555                                         break;
556                                 }
557                     folderTreeViewModel.initialize(account);
558                     groupTreeViewModel.initialize();
559                     updateStatistics();
560                 }
561             }
562
563             @Override
564             public void onError(Throwable t) {
565                 GWT.log("Error getting account", t);
566                 if (t instanceof RestException)
567                     displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
568                 else
569                     displayError("System error fetching user data: " + t.getMessage());
570             }
571         };
572         getAccount.setHeader("X-Auth-Token", token);
573         Scheduler.get().scheduleDeferred(getAccount);
574     }
575
576     protected void updateStatistics() {
577         totalFiles.setHTML(String.valueOf(account.getNumberOfObjects()));
578         usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
579         totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
580         usedPercent.setHTML(String.valueOf(account.getUsedPercentage()));
581         }
582
583         protected void createHomeContainer(final AccountResource account) {
584         String path = "/" + Pithos.HOME_CONTAINER;
585         PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
586             @Override
587             public void onSuccess(@SuppressWarnings("unused") Resource result) {
588                 if (!account.hasTrashContainer())
589                         createTrashContainer();
590                 else
591                         fetchAccount();
592             }
593
594             @Override
595             public void onError(Throwable t) {
596                 GWT.log("Error creating pithos", t);
597                 if (t instanceof RestException)
598                     displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
599                 else
600                     displayError("System error Error creating pithos: " + t.getMessage());
601             }
602         };
603         createPithos.setHeader("X-Auth-Token", getToken());
604         Scheduler.get().scheduleDeferred(createPithos);
605     }
606
607     protected void createTrashContainer() {
608         String path = "/" + Pithos.TRASH_CONTAINER;
609         PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
610             @Override
611             public void onSuccess(@SuppressWarnings("unused") Resource result) {
612                         fetchAccount();
613             }
614
615             @Override
616             public void onError(Throwable t) {
617                 GWT.log("Error creating pithos", t);
618                 if (t instanceof RestException)
619                     displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
620                 else
621                     displayError("System error Error creating pithos: " + t.getMessage());
622             }
623         };
624         createPithos.setHeader("X-Auth-Token", getToken());
625         Scheduler.get().scheduleDeferred(createPithos);
626     }
627
628     /**
629          * Creates an HTML fragment that places an image & caption together, for use
630          * in a group header.
631          *
632          * @param imageProto an image prototype for an image
633          * @param caption the group caption
634          * @return the header HTML fragment
635          */
636         private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
637                 String captionHTML = "<table class='caption' cellpadding='0' " 
638                 + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() 
639                 + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'>&nbsp;" 
640                 + caption + "</b></td></tr></table>";
641                 return captionHTML;
642         }
643
644         protected void onWindowResized(int height) {
645                 // Adjust the split panel to take up the available room in the window.
646                 int newHeight = height - splitPanel.getAbsoluteTop() - 60;
647                 if (newHeight < 1)
648                         newHeight = 1;
649                 splitPanel.setHeight("" + newHeight);
650                 inner.setHeight("" + newHeight);
651         }
652
653         @Override
654         public void onResize(ResizeEvent event) {
655                 int height = event.getHeight();
656                 onWindowResized(height);
657         }
658
659         /**
660          * Display an error message.
661          *
662          * @param msg the message to display
663          */
664         public void displayError(String msg) {
665                 messagePanel.displayError(msg);
666         }
667
668         /**
669          * Display a warning message.
670          *
671          * @param msg the message to display
672          */
673         public void displayWarning(String msg) {
674                 messagePanel.displayWarning(msg);
675         }
676
677         /**
678          * Display an informational message.
679          *
680          * @param msg the message to display
681          */
682         public void displayInformation(String msg) {
683                 messagePanel.displayInformation(msg);
684         }
685
686         /**
687          * Retrieve the fileList.
688          *
689          * @return the fileList
690          */
691         public FileList getFileList() {
692                 return fileList;
693         }
694
695         /**
696          * Retrieve the topPanel.
697          *
698          * @return the topPanel
699          */
700         TopPanel getTopPanel() {
701                 return topPanel;
702         }
703
704         /**
705          * Retrieve the clipboard.
706          *
707          * @return the clipboard
708          */
709         public Clipboard getClipboard() {
710                 return clipboard;
711         }
712
713         public StatusPanel getStatusPanel() {
714                 return statusPanel;
715         }
716
717         public String getToken() {
718                 return token;
719         }
720
721         public String getWebDAVPassword() {
722                 return webDAVPassword;
723         }
724
725         public static native void preventIESelection() /*-{
726                 $doc.body.onselectstart = function () { return false; };
727         }-*/;
728
729         public static native void enableIESelection() /*-{
730                 if ($doc.body.onselectstart != null)
731                 $doc.body.onselectstart = null;
732         }-*/;
733
734         /**
735          * @return the absolute path of the API root URL
736          */
737         public String getApiPath() {
738                 Configuration conf = (Configuration) GWT.create(Configuration.class);
739                 return conf.apiPath();
740         }
741
742         /**
743          * History support for folder navigation
744          * adds a new browser history entry
745          *
746          * @param key
747          */
748         public void updateHistory(String key){
749 //              Replace any whitespace of the initial string to "+"
750 //              String result = key.replaceAll("\\s","+");
751 //              Add a new browser history entry.
752 //              History.newItem(result);
753                 History.newItem(key);
754         }
755
756     public void deleteFolder(final Folder folder) {
757         String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + folder.getPrefix();
758         RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
759         builder.setHeader("If-Modified-Since", "0");
760         builder.setHeader("X-Auth-Token", getToken());
761         try {
762             builder.sendRequest("", new RequestCallback() {
763                 @Override
764                 public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
765                     if (response.getStatusCode() == Response.SC_OK) {
766                         JSONValue json = JSONParser.parseStrict(response.getText());
767                         JSONArray array = json.isArray();
768                         int i = 0;
769                         if (array != null) {
770                             deleteObject(folder, i, array);
771                         }
772                     }
773                 }
774
775                 @Override
776                 public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
777                     displayError("System error unable to delete folder: " + exception.getMessage());
778                 }
779             });
780         }
781         catch (RequestException e) {
782         }
783     }
784
785     public void deleteObject(final Folder folder, final int i, final JSONArray array) {
786         if (i < array.size()) {
787             JSONObject o = array.get(i).isObject();
788             if (o != null && !o.containsKey("subdir")) {
789                 JSONString name = o.get("name").isString();
790                 String path = "/" + folder.getContainer() + "/" + name.stringValue();
791                 DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), path) {
792                     @Override
793                     public void onSuccess(@SuppressWarnings("unused") Resource result) {
794                         deleteObject(folder, i + 1, array);
795                     }
796
797                     @Override
798                     public void onError(Throwable t) {
799                         GWT.log("", t);
800                         displayError("System error unable to delete folder: " + t.getMessage());
801                     }
802                 };
803                 delete.setHeader("X-Auth-Token", getToken());
804                 Scheduler.get().scheduleDeferred(delete);
805             }
806             else if (o != null) {
807                 String subdir = o.get("subdir").isString().stringValue();
808                 subdir = subdir.substring(0, subdir.length() - 1);
809                 String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + subdir;
810                 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
811                 builder.setHeader("If-Modified-Since", "0");
812                 builder.setHeader("X-Auth-Token", getToken());
813                 try {
814                     builder.sendRequest("", new RequestCallback() {
815                         @Override
816                         public void onResponseReceived(@SuppressWarnings("unused") Request request, Response response) {
817                             if (response.getStatusCode() == Response.SC_OK) {
818                                 JSONValue json = JSONParser.parseStrict(response.getText());
819                                 JSONArray array2 = json.isArray();
820                                 if (array2 != null) {
821                                     int l = array.size();
822                                     for (int j=0; j<array2.size(); j++) {
823                                         array.set(l++, array2.get(j));
824                                     }
825                                 }
826                                 deleteObject(folder, i + 1, array);
827                             }
828                         }
829
830                         @Override
831                         public void onError(@SuppressWarnings("unused") Request request, Throwable exception) {
832                             displayError("System error unable to delete folder: " + exception.getMessage());
833                         }
834                     });
835                 }
836                 catch (RequestException e) {
837                 }
838             }
839         }
840         else {
841             String path = folder.getUri();
842             DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), path) {
843                 @Override
844                 public void onSuccess(@SuppressWarnings("unused") Resource result) {
845                     updateFolder(folder.getParent(), true, null);
846                 }
847
848                 @Override
849                 public void onError(Throwable t) {
850                     GWT.log("", t);
851                     if (t instanceof RestException) {
852                         if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
853                                 displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
854                         else
855                                 onSuccess(null);
856                     }
857                     else
858                         displayError("System error unable to delete folder: " + t.getMessage());
859                 }
860             };
861             deleteFolder.setHeader("X-Auth-Token", getToken());
862             Scheduler.get().scheduleDeferred(deleteFolder);
863         }
864     }
865
866     public FolderTreeView getFolderTreeView() {
867         return folderTreeView;
868     }
869
870     public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
871         if (iter.hasNext()) {
872             File file = iter.next();
873             String path = targetUri + "/" + file.getName();
874             PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
875                 @Override
876                 public void onSuccess(@SuppressWarnings("unused") Resource result) {
877                     copyFiles(iter, targetUsername, targetUri, callback);
878                 }
879
880                 @Override
881                 public void onError(Throwable t) {
882                     GWT.log("", t);
883                     if (t instanceof RestException) {
884                         displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
885                     }
886                     else
887                         displayError("System error unable to copy file: "+t.getMessage());
888                 }
889             };
890             copyFile.setHeader("X-Auth-Token", getToken());
891             copyFile.setHeader("X-Copy-From", file.getUri());
892             Scheduler.get().scheduleDeferred(copyFile);
893         }
894         else  if (callback != null) {
895             callback.execute();
896         }
897     }
898
899     public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
900         if (iter.hasNext()) {
901             final Folder f = iter.next();
902             copyFolder(f, targetUsername, targetUri, callback);
903         }
904         else  if (callback != null) {
905             callback.execute();
906         }
907     }
908
909     public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
910         String path = targetUri + "/" + f.getName();
911         PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
912             @Override
913             public void onSuccess(@SuppressWarnings("unused") Resource result) {
914                 Iterator<File> iter = f.getFiles().iterator();
915                 copyFiles(iter, targetUsername, targetUri + "/" + f.getName(), new Command() {
916                     @Override
917                     public void execute() {
918                         Iterator<Folder> iterf = f.getSubfolders().iterator();
919                         copySubfolders(iterf, targetUsername, targetUri + "/" + f.getName(), new Command() {
920                             @Override
921                             public void execute() {
922                                 callback.execute();
923                             }
924                         });
925                     }
926                 });
927             }
928
929             @Override
930             public void onError(Throwable t) {
931                 GWT.log("", t);
932                 if (t instanceof RestException) {
933                     displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
934                 }
935                 else
936                     displayError("System error creating folder:" + t.getMessage());
937             }
938         };
939         createFolder.setHeader("X-Auth-Token", getToken());
940         createFolder.setHeader("Accept", "*/*");
941         createFolder.setHeader("Content-Length", "0");
942         createFolder.setHeader("Content-Type", "application/folder");
943         Scheduler.get().scheduleDeferred(createFolder);
944     }
945     
946     public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
947         selectionModels.add(model);
948     }
949
950         public OtherSharedTreeView getOtherSharedTreeView() {
951                 return otherSharedTreeView;
952         }
953
954         public void updateTrash(boolean showFiles, Command callback) {
955                 updateFolder(trash, showFiles, callback);
956         }
957
958         public void updateGroupsNode() {
959                 groupTreeView.updateGroupNode(null);
960         }
961
962         public void addGroup(String groupname) {
963                 Group newGroup = new Group(groupname);
964                 account.addGroup(newGroup);
965                 groupTreeView.updateGroupNode(null);
966         }
967
968         public void removeGroup(Group group) {
969                 account.removeGroup(group);
970                 updateGroupsNode();
971         }
972
973         public TreeView getSelectedTree() {
974                 return selectedTree;
975         }
976         
977         public Folder getSelection() {
978                 return selectedTree.getSelection();
979         }
980 }