Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / Pithos.java @ 4ea3c87b

History | View | Annotate | Download (45.1 kB)

1
/*
2
 * Copyright 2011-2012 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.mysharedtree.MysharedTreeView;
48
import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
49
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
50
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
51
import gr.grnet.pithos.web.client.rest.DeleteRequest;
52
import gr.grnet.pithos.web.client.rest.GetRequest;
53
import gr.grnet.pithos.web.client.rest.HeadRequest;
54
import gr.grnet.pithos.web.client.rest.PutRequest;
55
import gr.grnet.pithos.web.client.rest.RestException;
56

    
57
import java.util.ArrayList;
58
import java.util.HashMap;
59
import java.util.Iterator;
60
import java.util.List;
61
import java.util.Set;
62

    
63
import com.google.gwt.core.client.EntryPoint;
64
import com.google.gwt.core.client.GWT;
65
import com.google.gwt.core.client.Scheduler;
66
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
67
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
68
import com.google.gwt.event.dom.client.ClickEvent;
69
import com.google.gwt.event.dom.client.ClickHandler;
70
import com.google.gwt.event.logical.shared.ResizeEvent;
71
import com.google.gwt.event.logical.shared.ResizeHandler;
72
import com.google.gwt.http.client.Request;
73
import com.google.gwt.http.client.RequestBuilder;
74
import com.google.gwt.http.client.RequestCallback;
75
import com.google.gwt.http.client.RequestException;
76
import com.google.gwt.http.client.Response;
77
import com.google.gwt.http.client.URL;
78
import com.google.gwt.i18n.client.Dictionary;
79
import com.google.gwt.i18n.client.NumberFormat;
80
import com.google.gwt.json.client.JSONArray;
81
import com.google.gwt.json.client.JSONObject;
82
import com.google.gwt.json.client.JSONParser;
83
import com.google.gwt.json.client.JSONString;
84
import com.google.gwt.json.client.JSONValue;
85
import com.google.gwt.resources.client.ImageResource;
86
import com.google.gwt.resources.client.ImageResource.ImageOptions;
87
import com.google.gwt.user.client.Command;
88
import com.google.gwt.user.client.Cookies;
89
import com.google.gwt.user.client.Event;
90
import com.google.gwt.user.client.History;
91
import com.google.gwt.user.client.Window;
92
import com.google.gwt.user.client.ui.AbstractImagePrototype;
93
import com.google.gwt.user.client.ui.Button;
94
import com.google.gwt.user.client.ui.Composite;
95
import com.google.gwt.user.client.ui.HTML;
96
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
97
import com.google.gwt.user.client.ui.HasVerticalAlignment;
98
import com.google.gwt.user.client.ui.HorizontalPanel;
99
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
100
import com.google.gwt.user.client.ui.RootPanel;
101
import com.google.gwt.user.client.ui.VerticalPanel;
102
import com.google.gwt.view.client.SelectionChangeEvent;
103
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
104
import com.google.gwt.view.client.SingleSelectionModel;
105

    
106
/**
107
 * Entry point classes define <code>onModuleLoad()</code>.
108
 */
109
public class Pithos implements EntryPoint, ResizeHandler {
110

    
111
        public static final String HOME_CONTAINER = "pithos";
112

    
113
        public static final String TRASH_CONTAINER = "trash";
114

    
115
        public static final Configuration config = GWT.create(Configuration.class);
116
        
117
        /**
118
         * Instantiate an application-level image bundle. This object will provide
119
         * programmatic access to all the images needed by widgets.
120
         */
121
        static Images images = (Images) GWT.create(Images.class);
122

    
123
    public String getUsername() {
124
        return username;
125
    }
126

    
127
    public void setAccount(AccountResource acct) {
128
        account = acct;
129
    }
130

    
131
    public AccountResource getAccount() {
132
        return account;
133
    }
134

    
135
    public void updateFolder(Folder f, boolean showfiles, Command callback, final boolean openParent) {
136
        folderTreeView.updateFolder(f, showfiles, callback, openParent);
137
    }
138

    
139
    public void updateGroupNode(Group group) {
140
        groupTreeView.updateGroupNode(group);
141
    }
142

    
143
    public void updateMySharedRoot() {
144
            mysharedTreeView.updateRoot();
145
    }
146
    
147
    public void updateSharedFolder(Folder f, boolean showfiles) {
148
            mysharedTreeView.updateFolder(f, showfiles);
149
    }
150
    
151
    public void updateOtherSharedFolder(Folder f, boolean showfiles) {
152
            otherSharedTreeView.updateFolder(f, showfiles);
153
    }
154

    
155
    public MysharedTreeView getMySharedTreeView() {
156
        return mysharedTreeView;
157
    }
158

    
159
    /**
160
         * An aggregate image bundle that pulls together all the images for this
161
         * application into a single bundle.
162
         */
163
        public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
164

    
165
                @Source("gr/grnet/pithos/resources/document.png")
166
                ImageResource folders();
167

    
168
                @Source("gr/grnet/pithos/resources/advancedsettings.png")
169
                @ImageOptions(width=32, height=32)
170
                ImageResource tools();
171
        }
172

    
173
        private Throwable error;
174
        
175
        /**
176
         * The Application Clipboard implementation;
177
         */
178
        private Clipboard clipboard = new Clipboard();
179

    
180
        /**
181
         * The top panel that contains the menu bar.
182
         */
183
        private TopPanel topPanel;
184

    
185
        /**
186
         * The panel that contains the various system messages.
187
         */
188
        private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
189

    
190
        /**
191
         * The bottom panel that contains the status bar.
192
         */
193
        private StatusPanel statusPanel = null;
194

    
195
        /**
196
         * The file list widget.
197
         */
198
        private FileList fileList;
199

    
200
        /**
201
         * The tab panel that occupies the right side of the screen.
202
         */
203
        private VerticalPanel inner = new VerticalPanel();
204

    
205

    
206
        /**
207
         * The split panel that will contain the left and right panels.
208
         */
209
        private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
210

    
211
        /**
212
         * The currently selected item in the application, for use by the Edit menu
213
         * commands. Potential types are Folder, File, User and Group.
214
         */
215
        private Object currentSelection;
216

    
217
        public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
218

    
219
    private String username = null;
220

    
221
    /**
222
     * The authentication token of the current user.
223
     */
224
    private String token;
225

    
226
    VerticalPanel trees;
227
    
228
    SingleSelectionModel<Folder> folderTreeSelectionModel;
229
    FolderTreeViewModel folderTreeViewModel;
230
    FolderTreeView folderTreeView;
231

    
232
    SingleSelectionModel<Folder> mysharedTreeSelectionModel;
233
    MysharedTreeViewModel mysharedTreeViewModel;
234
    MysharedTreeView mysharedTreeView = null;;
235

    
236
    protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
237
    OtherSharedTreeViewModel otherSharedTreeViewModel;
238
    OtherSharedTreeView otherSharedTreeView = null;
239

    
240
    GroupTreeViewModel groupTreeViewModel;
241
    private GroupTreeView groupTreeView;
242

    
243
    TreeView selectedTree;
244
    protected AccountResource account;
245
    
246
    Folder trash;
247
    
248
    List<Composite> treeViews = new ArrayList<Composite>();
249

    
250
    @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
251
    
252
    Button upload;
253
    
254
    private HTML usedBytes;
255
    
256
    private HTML totalBytes;
257
    
258
    private HTML usedPercent;
259
    
260
    private HTML numOfFiles;
261
    
262
    private Toolbar toolbar;
263
    
264
    private FileUploadDialog fileUploadDialog;
265
    
266
        @Override
267
        public void onModuleLoad() {
268
                if (parseUserCredentials())
269
            initialize();
270
        }
271

    
272
    private void initialize() {
273
            boolean bareContent = Window.Location.getParameter("noframe") != null;
274
            String contentWidth = bareContent ? "100%" : "75%";
275

    
276
            VerticalPanel outer = new VerticalPanel();
277
        outer.setWidth("100%");
278
            if (!bareContent) {
279
                    outer.addStyleName("pithos-outer");
280
            }
281

    
282
        if (!bareContent) {
283
                topPanel = new TopPanel(this, Pithos.images);
284
                topPanel.setWidth("100%");
285
                outer.add(topPanel);
286
                outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
287
        }
288
        
289
        messagePanel.setVisible(false);
290
        outer.add(messagePanel);
291
        outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
292
        outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
293

    
294
        HorizontalPanel header = new HorizontalPanel();
295
        header.addStyleName("pithos-header");
296
        header.setWidth(contentWidth);
297
        if (bareContent)
298
                header.addStyleName("pithos-header-noframe");
299
        upload = new Button("Upload", new ClickHandler() {
300
            @Override
301
            public void onClick(ClickEvent event) {
302
                    if (getSelection() != null)
303
                            new UploadFileCommand(Pithos.this, null, getSelection()).execute();
304
            }
305
        });
306
        upload.addStyleName("pithos-uploadButton");
307
        header.add(upload);
308
        header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
309
        header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
310

    
311
        toolbar = new Toolbar(this);
312
        header.add(toolbar);
313
        header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
314
        header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
315
        
316
        HorizontalPanel folderStatistics = new HorizontalPanel();
317
        folderStatistics.addStyleName("pithos-folderStatistics");
318
        numOfFiles = new HTML();
319
        folderStatistics.add(numOfFiles);
320
        folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
321
        HTML numOfFilesLabel = new HTML("&nbsp;Files");
322
        folderStatistics.add(numOfFilesLabel);
323
        folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
324
        header.add(folderStatistics);
325
        header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
326
        header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
327
        header.setCellWidth(folderStatistics, "40px");
328
        outer.add(header);
329
        outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
330
        // Inner contains the various lists.nner
331
        inner.sinkEvents(Event.ONCONTEXTMENU);
332
        inner.setWidth("100%");
333

    
334
        folderTreeSelectionModel = new SingleSelectionModel<Folder>();
335
        folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
336
            @Override
337
            public void onSelectionChange(SelectionChangeEvent event) {
338
                if (folderTreeSelectionModel.getSelectedObject() != null) {
339
                    deselectOthers(folderTreeView, folderTreeSelectionModel);
340
                    applyPermissions(folderTreeSelectionModel.getSelectedObject());
341
                    Folder f = folderTreeSelectionModel.getSelectedObject();
342
                            updateFolder(f, true, new Command() {
343
                                    
344
                                    @Override
345
                                    public void execute() {
346
                                            updateStatistics();
347
                                    }
348
                            }, true);
349
                            showRelevantToolbarButtons();
350
                }
351
                                else {
352
                                        if (getSelectedTree().equals(folderTreeView))
353
                                                setSelectedTree(null);
354
                                        if (getSelectedTree() == null)
355
                                                showRelevantToolbarButtons();
356
                                }
357
            }
358
        });
359
        selectionModels.add(folderTreeSelectionModel);
360

    
361
        folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
362
        folderTreeView = new FolderTreeView(folderTreeViewModel);
363
        treeViews.add(folderTreeView);
364
        
365
        fileList = new FileList(this, images, folderTreeView);
366
        inner.add(fileList);
367

    
368
        groupTreeViewModel = new GroupTreeViewModel(this);
369
        groupTreeView = new GroupTreeView(groupTreeViewModel);
370
        treeViews.add(groupTreeView);
371
        
372
        trees = new VerticalPanel();
373
        trees.setWidth("100%");
374

    
375
        
376
        HorizontalPanel treeHeader = new HorizontalPanel();
377
        treeHeader.addStyleName("pithos-treeHeader");
378
        treeHeader.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
379
        treeHeader.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
380
        HorizontalPanel statistics = new HorizontalPanel();
381
        statistics.addStyleName("pithos-statistics");
382
        statistics.add(new HTML("Used:&nbsp;"));
383
        usedBytes = new HTML();
384
        statistics.add(usedBytes);
385
        statistics.add(new HTML("&nbsp;of&nbsp;"));
386
        totalBytes = new HTML();
387
        statistics.add(totalBytes);
388
        statistics.add(new HTML("&nbsp;("));
389
        usedPercent = new HTML();
390
        statistics.add(usedPercent);
391
        statistics.add(new HTML(")"));
392
        treeHeader.add(statistics);
393
        treeHeader.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_LEFT);
394
        trees.add(treeHeader);
395

    
396
        trees.add(folderTreeView);
397
        trees.add(groupTreeView);
398
        // Add the left and right panels to the split panel.
399
        splitPanel.setLeftWidget(trees);
400
        splitPanel.setRightWidget(inner);
401
        splitPanel.setSplitPosition("35%");
402
        splitPanel.setSize("100%", "100%");
403
        splitPanel.addStyleName("pithos-splitPanel");
404
        splitPanel.setWidth(contentWidth);
405
        outer.add(splitPanel);
406
        outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
407

    
408
        if (!bareContent) {
409
                statusPanel = new StatusPanel();
410
                statusPanel.setWidth("100%");
411
                outer.add(statusPanel);
412
                outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
413
        }
414
        else
415
                splitPanel.addStyleName("pithos-splitPanel-noframe");
416

    
417
        // Hook the window resize event, so that we can adjust the UI.
418
        Window.addResizeHandler(this);
419
        // Clear out the window's built-in margin, because we want to take
420
        // advantage of the entire client area.
421
        Window.setMargin("0px");
422
        // Finally, add the outer panel to the RootPanel, so that it will be
423
        // displayed.
424
        RootPanel.get().add(outer);
425
        // Call the window resized handler to get the initial sizes setup. Doing
426
        // this in a deferred command causes it to occur after all widgets'
427
        // sizes have been computed by the browser.
428
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
429

    
430
            @Override
431
            public void execute() {
432
                onWindowResized(Window.getClientHeight());
433
            }
434
        });
435

    
436
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
437
            @Override
438
            public void execute() {
439
                fetchAccount(new Command() {
440
                                        
441
                                        @Override
442
                                        public void execute() {
443
                                if (!account.hasHomeContainer())
444
                                    createHomeContainer(account, this);
445
                                else if (!account.hasTrashContainer())
446
                                        createTrashContainer(this);
447
                                else {
448
                                        for (Folder f : account.getContainers())
449
                                                if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
450
                                                        trash = f;
451
                                                        break;
452
                                                }
453
                                    folderTreeViewModel.initialize(account, new Command() {
454
                                                                
455
                                                                @Override
456
                                                                public void execute() {
457
                                                    createMySharedTree();
458
                                                                }
459
                                                        });
460
                                    groupTreeViewModel.initialize();
461
                                    showStatistics();
462
                                }
463
                                        }
464
                                });
465
            }
466
        });
467
        
468
        Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
469
                        
470
                        @Override
471
                        public boolean execute() {
472
                                Folder f = getSelection();
473
                                if (f != null) {
474
                                        if (getSelectedTree().equals(folderTreeView))
475
                                                updateFolder(f, true, null, false);
476
                                        else if (getSelectedTree().equals(mysharedTreeView))
477
                                                updateSharedFolder(f, true);
478
                                }
479
                                return true;
480
                        }
481
                }, 3000);
482
    }
483

    
484
    public void applyPermissions(Folder f) {
485
            if (f != null) {
486
                    if (f.isInTrash())
487
                            upload.setEnabled(false);
488
                    else {
489
                            Boolean[] perms = f.getPermissions().get(username);
490
                            if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
491
                                    upload.setEnabled(true);
492
                            }
493
                            else
494
                                    upload.setEnabled(false);
495
                    }
496
            }
497
            else
498
                    upload.setEnabled(false);
499
        }
500

    
501
        @SuppressWarnings({ "rawtypes", "unchecked" })
502
        public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
503
            selectedTree = _selectedTree;
504
            
505
            for (Composite c : treeViews)
506
                    if (c.equals(selectedTree))
507
                            c.addStyleName("cellTreeWidget-selectedTree");
508
                    else
509
                            c.removeStyleName("cellTreeWidget-selectedTree");
510
            
511
        for (SingleSelectionModel s : selectionModels)
512
            if (!s.equals(model) && s.getSelectedObject() != null)
513
                s.setSelected(s.getSelectedObject(), false);
514
    }
515

    
516
    public void showFiles(final Folder f) {
517
        Set<File> files = f.getFiles();
518
        showFiles(files);
519
    }
520

    
521
    public void showFiles(Set<File> files) {
522
        fileList.setFiles(new ArrayList<File>(files));
523
    }
524

    
525
    /**
526
         * Parse and store the user credentials to the appropriate fields.
527
         */
528
        private boolean parseUserCredentials() {
529
        Configuration conf = (Configuration) GWT.create(Configuration.class);
530
                Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
531
        String cookie = otherProperties.get("authCookie");
532
        String auth = Cookies.getCookie(cookie);
533
        if (auth == null) {
534
            authenticateUser();
535
            return false;
536
        }
537
        if (auth.startsWith("\""))
538
                auth = auth.substring(1);
539
        if (auth.endsWith("\""))
540
                auth = auth.substring(0, auth.length() - 1);
541
                String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
542
                if (authSplit.length != 2) {
543
                    authenticateUser();
544
                    return false;
545
                }
546
                username = authSplit[0];
547
                token = authSplit[1];
548

    
549
        String gotoUrl = Window.Location.getParameter("goto");
550
                if (gotoUrl != null && gotoUrl.length() > 0) {
551
                        Window.Location.assign(gotoUrl);
552
                        return false;
553
                }
554
                return true;
555
    }
556

    
557
    /**
558
         * Redirect the user to the login page for authentication.
559
         */
560
        protected void authenticateUser() {
561
                Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
562
        Window.Location.assign(otherProperties.get("loginUrl") + Window.Location.getHref());
563
        }
564

    
565
        protected void fetchAccount(final Command callback) {
566
        String path = "?format=json";
567

    
568
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
569
            @Override
570
            public void onSuccess(AccountResource _result) {
571
                account = _result;
572
                if (callback != null)
573
                        callback.execute();
574
            }
575

    
576
            @Override
577
            public void onError(Throwable t) {
578
                GWT.log("Error getting account", t);
579
                                setError(t);
580
                if (t instanceof RestException)
581
                    displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
582
                else
583
                    displayError("System error fetching user data: " + t.getMessage());
584
            }
585

    
586
                        @Override
587
                        protected void onUnauthorized(Response response) {
588
                                sessionExpired();
589
                        }
590
        };
591
        getAccount.setHeader("X-Auth-Token", token);
592
        Scheduler.get().scheduleDeferred(getAccount);
593
    }
594

    
595
    public void updateStatistics() {
596
            HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
597

    
598
                        @Override
599
                        public void onSuccess(AccountResource _result) {
600
                                showStatistics();
601
                        }
602

    
603
                        @Override
604
                        public void onError(Throwable t) {
605
                GWT.log("Error getting account", t);
606
                                setError(t);
607
                if (t instanceof RestException)
608
                    displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
609
                else
610
                    displayError("System error fetching user data: " + t.getMessage());
611
                        }
612

    
613
                        @Override
614
                        protected void onUnauthorized(Response response) {
615
                                sessionExpired();
616
                        }
617
                };
618
                headAccount.setHeader("X-Auth-Token", token);
619
                Scheduler.get().scheduleDeferred(headAccount);
620
        }
621

    
622
        protected void showStatistics() {
623
            usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
624
            totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
625
            NumberFormat nf = NumberFormat.getPercentFormat();
626
            usedPercent.setHTML(nf.format(account.getUsedPercentage()));
627
        }
628

    
629
        protected void createHomeContainer(final AccountResource _account, final Command callback) {
630
        String path = "/" + Pithos.HOME_CONTAINER;
631
        PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
632
            @Override
633
            public void onSuccess(Resource result) {
634
                    if (!_account.hasTrashContainer())
635
                            createTrashContainer(callback);
636
                    else
637
                            fetchAccount(callback);
638
            }
639

    
640
            @Override
641
            public void onError(Throwable t) {
642
                GWT.log("Error creating pithos", t);
643
                                setError(t);
644
                if (t instanceof RestException)
645
                    displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
646
                else
647
                    displayError("System error Error creating pithos: " + t.getMessage());
648
            }
649

    
650
                        @Override
651
                        protected void onUnauthorized(Response response) {
652
                                sessionExpired();
653
                        }
654
        };
655
        createPithos.setHeader("X-Auth-Token", getToken());
656
        Scheduler.get().scheduleDeferred(createPithos);
657
    }
658

    
659
    protected void createTrashContainer(final Command callback) {
660
        String path = "/" + Pithos.TRASH_CONTAINER;
661
        PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
662
            @Override
663
            public void onSuccess(Resource result) {
664
                           fetchAccount(callback);
665
            }
666

    
667
            @Override
668
            public void onError(Throwable t) {
669
                GWT.log("Error creating pithos", t);
670
                                setError(t);
671
                if (t instanceof RestException)
672
                    displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
673
                else
674
                    displayError("System error Error creating pithos: " + t.getMessage());
675
            }
676

    
677
                        @Override
678
                        protected void onUnauthorized(Response response) {
679
                                sessionExpired();
680
                        }
681
        };
682
        createPithos.setHeader("X-Auth-Token", getToken());
683
        Scheduler.get().scheduleDeferred(createPithos);
684
    }
685

    
686
    /**
687
         * Creates an HTML fragment that places an image & caption together, for use
688
         * in a group header.
689
         *
690
         * @param imageProto an image prototype for an image
691
         * @param caption the group caption
692
         * @return the header HTML fragment
693
         */
694
        private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
695
                String captionHTML = "<table class='caption' cellpadding='0' " 
696
                + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() 
697
                + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'>&nbsp;" 
698
                + caption + "</b></td></tr></table>";
699
                return captionHTML;
700
        }
701

    
702
        protected void onWindowResized(int height) {
703
                // Adjust the split panel to take up the available room in the window.
704
                int newHeight = height - splitPanel.getAbsoluteTop();
705
                if (newHeight < 1)
706
                        newHeight = 1;
707
                splitPanel.setHeight("" + newHeight);
708
                inner.setHeight("" + newHeight);
709
        }
710

    
711
        @Override
712
        public void onResize(ResizeEvent event) {
713
                int height = event.getHeight();
714
                onWindowResized(height);
715
        }
716

    
717
        /**
718
         * Display an error message.
719
         *
720
         * @param msg the message to display
721
         */
722
        public void displayError(String msg) {
723
                messagePanel.displayError(msg);
724
        }
725

    
726
        /**
727
         * Display a warning message.
728
         *
729
         * @param msg the message to display
730
         */
731
        public void displayWarning(String msg) {
732
                messagePanel.displayWarning(msg);
733
        }
734

    
735
        /**
736
         * Display an informational message.
737
         *
738
         * @param msg the message to display
739
         */
740
        public void displayInformation(String msg) {
741
                messagePanel.displayInformation(msg);
742
        }
743

    
744
        /**
745
         * Retrieve the fileList.
746
         *
747
         * @return the fileList
748
         */
749
        public FileList getFileList() {
750
                return fileList;
751
        }
752

    
753
        /**
754
         * Retrieve the topPanel.
755
         *
756
         * @return the topPanel
757
         */
758
        TopPanel getTopPanel() {
759
                return topPanel;
760
        }
761

    
762
        /**
763
         * Retrieve the clipboard.
764
         *
765
         * @return the clipboard
766
         */
767
        public Clipboard getClipboard() {
768
                return clipboard;
769
        }
770

    
771
        public StatusPanel getStatusPanel() {
772
                return statusPanel;
773
        }
774

    
775
        public String getToken() {
776
                return token;
777
        }
778

    
779
        public static native void preventIESelection() /*-{
780
                $doc.body.onselectstart = function () { return false; };
781
        }-*/;
782

    
783
        public static native void enableIESelection() /*-{
784
                if ($doc.body.onselectstart != null)
785
                $doc.body.onselectstart = null;
786
        }-*/;
787

    
788
        /**
789
         * @return the absolute path of the API root URL
790
         */
791
        public String getApiPath() {
792
                Configuration conf = (Configuration) GWT.create(Configuration.class);
793
                return conf.apiPath();
794
        }
795

    
796
        /**
797
         * History support for folder navigation
798
         * adds a new browser history entry
799
         *
800
         * @param key
801
         */
802
        public void updateHistory(String key){
803
//                Replace any whitespace of the initial string to "+"
804
//                String result = key.replaceAll("\\s","+");
805
//                Add a new browser history entry.
806
//                History.newItem(result);
807
                History.newItem(key);
808
        }
809

    
810
    public void deleteFolder(final Folder folder, final Command callback) {
811
        String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
812
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
813
        builder.setHeader("X-Auth-Token", getToken());
814
        try {
815
            builder.sendRequest("", new RequestCallback() {
816
                @Override
817
                public void onResponseReceived(Request request, Response response) {
818
                    if (response.getStatusCode() == Response.SC_OK) {
819
                        JSONValue json = JSONParser.parseStrict(response.getText());
820
                        JSONArray array = json.isArray();
821
                        int i = 0;
822
                        if (array != null) {
823
                            deleteObject(folder, i, array, callback);
824
                        }
825
                    }
826
                }
827

    
828
                @Override
829
                public void onError(Request request, Throwable exception) {
830
                        setError(exception);
831
                    displayError("System error unable to delete folder: " + exception.getMessage());
832
                }
833
            });
834
        }
835
        catch (RequestException e) {
836
        }
837
    }
838

    
839
    void deleteObject(final Folder folder, final int i, final JSONArray array, final Command callback) {
840
        if (i < array.size()) {
841
            JSONObject o = array.get(i).isObject();
842
            if (o != null && !o.containsKey("subdir")) {
843
                JSONString name = o.get("name").isString();
844
                String path = "/" + folder.getContainer() + "/" + name.stringValue();
845
                DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), URL.encode(path)) {
846
                    @Override
847
                    public void onSuccess(Resource result) {
848
                        deleteObject(folder, i + 1, array, callback);
849
                    }
850

    
851
                    @Override
852
                    public void onError(Throwable t) {
853
                        GWT.log("", t);
854
                                                setError(t);
855
                        displayError("System error unable to delete folder: " + t.getMessage());
856
                    }
857

    
858
                                    @Override
859
                                    protected void onUnauthorized(Response response) {
860
                                            sessionExpired();
861
                                    }
862
                };
863
                delete.setHeader("X-Auth-Token", getToken());
864
                Scheduler.get().scheduleDeferred(delete);
865
            }
866
            else if (o != null) {
867
                String subdir = o.get("subdir").isString().stringValue();
868
                subdir = subdir.substring(0, subdir.length() - 1);
869
                String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
870
                RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
871
                builder.setHeader("X-Auth-Token", getToken());
872
                try {
873
                    builder.sendRequest("", new RequestCallback() {
874
                        @Override
875
                        public void onResponseReceived(Request request, Response response) {
876
                            if (response.getStatusCode() == Response.SC_OK) {
877
                                JSONValue json = JSONParser.parseStrict(response.getText());
878
                                JSONArray array2 = json.isArray();
879
                                if (array2 != null) {
880
                                    int l = array.size();
881
                                    for (int j=0; j<array2.size(); j++) {
882
                                        array.set(l++, array2.get(j));
883
                                    }
884
                                }
885
                                deleteObject(folder, i + 1, array, callback);
886
                            }
887
                        }
888

    
889
                        @Override
890
                        public void onError(Request request, Throwable exception) {
891
                                setError(exception);
892
                            displayError("System error unable to delete folder: " + exception.getMessage());
893
                        }
894
                    });
895
                }
896
                catch (RequestException e) {
897
                }
898
            }
899
        }
900
        else {
901
            String path = folder.getUri();
902
            DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), URL.encode(path)) {
903
                @Override
904
                public void onSuccess(Resource result) {
905
                    updateFolder(folder.getParent(), true, new Command() {
906
                                                
907
                                                @Override
908
                                                public void execute() {
909
                                                        folderTreeSelectionModel.setSelected(folder.getParent(), true);
910
                                                        updateStatistics();
911
                                                        if (callback != null)
912
                                                                callback.execute();
913
                                                }
914
                                        }, true);
915
                }
916

    
917
                @Override
918
                public void onError(Throwable t) {
919
                    GWT.log("", t);
920
                                        setError(t);
921
                    if (t instanceof RestException) {
922
                            if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
923
                                    displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
924
                            else
925
                                    onSuccess(null);
926
                    }
927
                    else
928
                        displayError("System error unable to delete folder: " + t.getMessage());
929
                }
930

    
931
                                @Override
932
                                protected void onUnauthorized(Response response) {
933
                                        sessionExpired();
934
                                }
935
            };
936
            deleteFolder.setHeader("X-Auth-Token", getToken());
937
            Scheduler.get().scheduleDeferred(deleteFolder);
938
        }
939
    }
940

    
941
    public FolderTreeView getFolderTreeView() {
942
        return folderTreeView;
943
    }
944

    
945
    public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
946
        if (iter.hasNext()) {
947
            File file = iter.next();
948
            String path = targetUri + "/" + file.getName();
949
            PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
950
                @Override
951
                public void onSuccess(Resource result) {
952
                    copyFiles(iter, targetUsername, targetUri, callback);
953
                }
954

    
955
                @Override
956
                public void onError(Throwable t) {
957
                    GWT.log("", t);
958
                                        setError(t);
959
                    if (t instanceof RestException) {
960
                        displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
961
                    }
962
                    else
963
                        displayError("System error unable to copy file: "+t.getMessage());
964
                }
965

    
966
                                @Override
967
                                protected void onUnauthorized(Response response) {
968
                                        sessionExpired();
969
                                }
970
            };
971
            copyFile.setHeader("X-Auth-Token", getToken());
972
            copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
973
            if (!file.getOwner().equals(targetUsername))
974
                    copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwner()));
975
            copyFile.setHeader("Content-Type", file.getContentType());
976
            Scheduler.get().scheduleDeferred(copyFile);
977
        }
978
        else  if (callback != null) {
979
            callback.execute();
980
        }
981
    }
982

    
983
    public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
984
        if (iter.hasNext()) {
985
            final Folder f = iter.next();
986
            copyFolder(f, targetUsername, targetUri, new Command() {
987
                                
988
                                @Override
989
                                public void execute() {
990
                                        copySubfolders(iter, targetUsername, targetUri, callback);
991
                                }
992
                        });
993
        }
994
        else  if (callback != null) {
995
            callback.execute();
996
        }
997
    }
998

    
999
    public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
1000
        String path = targetUri + "/" + f.getName();
1001
        PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
1002
            @Override
1003
            public void onSuccess(Resource result) {
1004
                    GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
1005

    
1006
                                        @Override
1007
                                        public void onSuccess(final Folder _f) {
1008
                                Iterator<File> iter = _f.getFiles().iterator();
1009
                                copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
1010
                                    @Override
1011
                                    public void execute() {
1012
                                        Iterator<Folder> iterf = _f.getSubfolders().iterator();
1013
                                        copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1014
                                    }
1015
                                });
1016
                                        }
1017

    
1018
                                        @Override
1019
                                        public void onError(Throwable t) {
1020
                                GWT.log("", t);
1021
                                                setError(t);
1022
                                if (t instanceof RestException) {
1023
                                    displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1024
                                }
1025
                                else
1026
                                    displayError("System error getting folder: " + t.getMessage());
1027
                                        }
1028

    
1029
                                        @Override
1030
                                        protected void onUnauthorized(Response response) {
1031
                                                sessionExpired();
1032
                                        }
1033
                                };
1034
                                getFolder.setHeader("X-Auth-Token", getToken());
1035
                                Scheduler.get().scheduleDeferred(getFolder);
1036
            }
1037

    
1038
            @Override
1039
            public void onError(Throwable t) {
1040
                GWT.log("", t);
1041
                                setError(t);
1042
               if (t instanceof RestException) {
1043
                    displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1044
                }
1045
                else
1046
                    displayError("System error creating folder: " + t.getMessage());
1047
            }
1048

    
1049
                        @Override
1050
                        protected void onUnauthorized(Response response) {
1051
                                sessionExpired();
1052
                        }
1053
        };
1054
        createFolder.setHeader("X-Auth-Token", getToken());
1055
        createFolder.setHeader("Accept", "*/*");
1056
        createFolder.setHeader("Content-Length", "0");
1057
        createFolder.setHeader("Content-Type", "application/folder");
1058
        Scheduler.get().scheduleDeferred(createFolder);
1059
    }
1060
    
1061
    public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1062
            selectionModels.add(model);
1063
    }
1064

    
1065
        public OtherSharedTreeView getOtherSharedTreeView() {
1066
                return otherSharedTreeView;
1067
        }
1068

    
1069
        public void updateTrash(boolean showFiles, Command callback) {
1070
                updateFolder(trash, showFiles, callback, true);
1071
        }
1072

    
1073
        public void updateGroupsNode() {
1074
                groupTreeView.updateGroupNode(null);
1075
        }
1076

    
1077
        public void addGroup(String groupname) {
1078
                Group newGroup = new Group(groupname);
1079
                account.addGroup(newGroup);
1080
                groupTreeView.updateGroupNode(null);
1081
        }
1082

    
1083
        public void removeGroup(Group group) {
1084
                account.removeGroup(group);
1085
                updateGroupsNode();
1086
        }
1087

    
1088
        public TreeView getSelectedTree() {
1089
                return selectedTree;
1090
        }
1091
        
1092
        public void setSelectedTree(TreeView selected) {
1093
                selectedTree = selected;
1094
        }
1095

    
1096
        public Folder getSelection() {
1097
                return selectedTree.getSelection();
1098
        }
1099

    
1100
        public void showFolderStatistics(int folderFileCount) {
1101
                numOfFiles.setHTML(String.valueOf(folderFileCount));
1102
        }
1103

    
1104
        public GroupTreeView getGroupTreeView() {
1105
                return groupTreeView;
1106
        }
1107

    
1108
        public void sessionExpired() {
1109
                new SessionExpiredDialog(this).center();
1110
        }
1111

    
1112
        public void updateRootFolder(Command callback) {
1113
                updateFolder(account.getPithos(), false, callback, true);
1114
        }
1115

    
1116
        void createMySharedTree() {
1117
                mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1118
                mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1119
                    @Override
1120
                    public void onSelectionChange(SelectionChangeEvent event) {
1121
                        if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1122
                            deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1123
                            upload.setEnabled(false);
1124
                            updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1125
                                        showRelevantToolbarButtons();
1126
                        }
1127
                                else {
1128
                                        if (getSelectedTree().equals(mysharedTreeView))
1129
                                                setSelectedTree(null);
1130
                                        if (getSelectedTree() == null)
1131
                                                showRelevantToolbarButtons();
1132
                                }
1133
                     }
1134
                });
1135
                selectionModels.add(mysharedTreeSelectionModel);
1136
                mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1137
                mysharedTreeViewModel.initialize(new Command() {
1138
                        
1139
                        @Override
1140
                        public void execute() {
1141
                            mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1142
                                trees.insert(mysharedTreeView, 2);
1143
                                treeViews.add(mysharedTreeView);
1144
                                createOtherSharedTree();
1145
                        }
1146
                });
1147
        }
1148

    
1149
        void createOtherSharedTree() {
1150
                otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1151
                otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1152
                    @Override
1153
                    public void onSelectionChange(SelectionChangeEvent event) {
1154
                        if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1155
                            deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1156
                            otherSharedTreeView.addStyleName("cellTreeWidget-selectedTree");
1157
                            applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1158
                            updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1159
                                        showRelevantToolbarButtons();
1160
                        }
1161
                                else {
1162
                                        if (getSelectedTree().equals(otherSharedTreeView))
1163
                                                setSelectedTree(null);
1164
                                        if (getSelectedTree() == null)
1165
                                                showRelevantToolbarButtons();
1166
                                }
1167
                     }
1168
                });
1169
                selectionModels.add(otherSharedTreeSelectionModel);
1170
                otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1171
                otherSharedTreeViewModel.initialize(new Command() {
1172
                        
1173
                        @Override
1174
                        public void execute() {
1175
                            otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1176
                                trees.insert(otherSharedTreeView, 3);
1177
                                treeViews.add(otherSharedTreeView);
1178
                        }
1179
                });
1180
        }
1181

    
1182
        public native void log1(String message)/*-{
1183
                $wnd.console.log(message);
1184
        }-*/;
1185

    
1186
        public String getErrorData() {
1187
                if (error != null)
1188
                        return error.toString();
1189
                return "";
1190
        }
1191
        
1192
        public void setError(Throwable t) {
1193
                error = t;
1194
        }
1195
        
1196
        public void showRelevantToolbarButtons() {
1197
                toolbar.showRelevantButtons();
1198
        }
1199

    
1200
        public FileUploadDialog getFileUploadDialog() {
1201
                if (fileUploadDialog == null)
1202
                        fileUploadDialog = new FileUploadDialog(this);
1203
                return fileUploadDialog;
1204
        }
1205

    
1206
        public void hideUploadIndicator() {
1207
                upload.removeStyleName("pithos-uploadButton-loading");
1208
                upload.setTitle("");
1209
        }
1210
        
1211
        public void showUploadIndicator() {
1212
                upload.addStyleName("pithos-uploadButton-loading");
1213
                upload.setTitle("Upload in progress. Click for details.");
1214
        }
1215

    
1216
        public void scheduleFolderHeadCommand(final Folder folder, final Command callback) {
1217
                if (folder == null) {
1218
                        if (callback != null)
1219
                                callback.execute();
1220
                }
1221
                else {
1222
                        HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, getApiPath(), folder.getOwner(), folder.getUri(), folder) {
1223
        
1224
                                @Override
1225
                                public void onSuccess(Folder _result) {
1226
                                        if (callback != null)
1227
                                                callback.execute();
1228
                                }
1229
        
1230
                                @Override
1231
                                public void onError(Throwable t) {
1232
                                if (t instanceof RestException) {
1233
                                        if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
1234
                                final String path = folder.getUri();
1235
                                PutRequest newFolder = new PutRequest(getApiPath(), folder.getOwner(), path) {
1236
                                    @Override
1237
                                    public void onSuccess(Resource _result) {
1238
                                            scheduleFolderHeadCommand(folder, callback);
1239
                                    }
1240
        
1241
                                    @Override
1242
                                    public void onError(Throwable _t) {
1243
                                        GWT.log("", _t);
1244
                                                                setError(_t);
1245
                                        if(_t instanceof RestException){
1246
                                            displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
1247
                                        }
1248
                                        else
1249
                                            displayError("System error creating folder: " + _t.getMessage());
1250
                                    }
1251
        
1252
                                                    @Override
1253
                                                    protected void onUnauthorized(Response response) {
1254
                                                            sessionExpired();
1255
                                                    }
1256
                                };
1257
                                newFolder.setHeader("X-Auth-Token", getToken());
1258
                                newFolder.setHeader("Content-Type", "application/folder");
1259
                                newFolder.setHeader("Accept", "*/*");
1260
                                newFolder.setHeader("Content-Length", "0");
1261
                                Scheduler.get().scheduleDeferred(newFolder);
1262
                                        }
1263
                                        else
1264
                                                displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
1265
                                }
1266
                                else
1267
                                    displayError("System error heading folder: " + t.getMessage());
1268
        
1269
                                GWT.log("Error heading folder", t);
1270
                                        setError(t);
1271
                                }
1272
        
1273
                                @Override
1274
                                protected void onUnauthorized(Response response) {
1275
                                        sessionExpired();
1276
                                }
1277
                        };
1278
                        headFolder.setHeader("X-Auth-Token", getToken());
1279
                        Scheduler.get().scheduleDeferred(headFolder);
1280
                }
1281
        }
1282

    
1283
        public void scheduleFileHeadCommand(File f, final Command callback) {
1284
                HeadRequest<File> headFile = new HeadRequest<File>(File.class, getApiPath(), f.getOwner(), f.getUri(), f) {
1285

    
1286
                        @Override
1287
                        public void onSuccess(File _result) {
1288
                                if (callback != null)
1289
                                        callback.execute();
1290
                        }
1291

    
1292
                        @Override
1293
                        public void onError(Throwable t) {
1294
                        GWT.log("Error heading file", t);
1295
                                setError(t);
1296
                        if (t instanceof RestException)
1297
                            displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
1298
                        else
1299
                            displayError("System error heading file: " + t.getMessage());
1300
                        }
1301

    
1302
                        @Override
1303
                        protected void onUnauthorized(Response response) {
1304
                                sessionExpired();
1305
                        }
1306
                };
1307
                headFile.setHeader("X-Auth-Token", getToken());
1308
                Scheduler.get().scheduleDeferred(headFile);
1309
        }
1310
}