Welcome to mirror list, hosted at ThFree Co, Russian Federation.

restoredialog.js « scripts « greeno « webroot « Server « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0561b1fe43a08e7ad09b3ce4c68e3e545a5b0e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
$(document).ready(function() {

    var curpage = 0;
    var backupId = 0;
    var dirSep = '/';
    var pathSep = ':';
    var trees = {};
    var searchdata = {};
    var nodedata = {};

    var manualIncludes = {};
    var searchTrees = {};
    var commonPrefix = [];

    var dbversions = {};
    var isDirectRestore = false;

    var includeMap = null;

    var performRestore = function(tasks) {
        var dlg = $('<div></div>').attr('title', 'Restoring files ...');
        var cancelling = false;
        var currentProgressTask = null;
        var remainingTasks = tasks.length;

        dlg.dialog({
            autoOpen: true,
            modal: true,
            closeOnEscape: false,
            buttons: [
                { text: 'Cancel', click: function(event, ui) {
                    if (confirm('Stop the restore?')) {
                        cancelling = true;
                        if (currentProgressTask != null)
                            APP_DATA.stopTask(currentProgressTask.taskId, true);

                        updatePageNav();
                        dlg.dialog('close');
                        dlg.remove();
                    }
                }}
            ]
        });

        dlg.parent().find('.ui-dialog-titlebar-close').remove().first().remove();

        var pg = $('<div></div>');
        pg.progressbar({ value: false });
        var pgtxt = $('<div></div>');
        pgtxt.text('Sending jobs to server ...');

        dlg.append(pg);
        dlg.append(pgtxt);

        var onAllRestoresCompleted = function() {
            $(document).off('server-progress-updated', serverProgressUpdateMethod);
            $(document).off('server-state-updated', serverStateUpdateMethod);

            curpage = Math.min(2, curpage+1);
            updatePageNav();
            dlg.dialog('close');
            dlg.remove();
        };

        var serverProgressUpdateMethod = function(e, data) {
            if (currentProgressTask == null)
                pgtxt.text('Waiting for restore to begin ...');
            else {
                if (tasks.length == 1)
                    pgtxt.text('Restoring files ...');
                else
                    pgtxt.text('Restoring files (' + (tasks.length - remainingTasks) + ' of ' + tasks.length + ')');
            }

        };

        var serverStateUpdateMethod = function(e, data) {
            var activeTaskId = -1;
            var queuedTasks = [];

            if (cancelling)
                return;

            if (data.ActiveTask != null)
                activeTaskId = data.ActiveTask.Item1;

            if (data.SchedulerQueueIds != null)
                for(var n in data.SchedulerQueueIds)
                    queuedTasks.push(data.SchedulerQueueIds[n].Item1);

            var remaining = 0;
            for(var n in tasks) {

                if (tasks[n].taskId == activeTaskId) {
                    currentProgressTask = tasks[n];
                    remaining++;
                    continue;
                }

                for(var i in queuedTasks)
                    if (queuedTasks[i] == tasks[n].taskId) {
                        remaining++;
                        continue;
                    }
            }

            if (remaining == 0)
                onAllRestoresCompleted();
        };

        var curTask = 0;
        var registerTasks = function() {
            if (curTask == tasks.length) {
                pgtxt.text('Waiting for restore to begin ...');
                $(document).on('server-progress-updated', serverProgressUpdateMethod);
                $(document).on('server-state-updated', serverStateUpdateMethod);
                return;
            }

            var task = tasks[curTask];
            curTask++;

            APP_DATA.callServer(task, function(data) {
                task['taskId'] = data.TaskID;
                registerTasks();
            },
            function(d, s, m) {
                alert('Error: ' + m);
                dlg.dialog('close');
                dlg.remove();
            });

        }

        registerTasks();
    };

    $('#restore-dialog').dialog({
        minWidth: 320, 
        width: $('body').width > 600 ? 320 : 600, 
        minHeight: 480, 
        height: 500, 
        modal: true,
        autoOpen: false,
        closeOnEscape: true,
        buttons: [
            { text: '< Previous', disabled: true, click: function(event, ui) {
                curpage = Math.max(0, curpage-1);
                updatePageNav();

            }},
            { text: 'Next >', click: function(event, ui) {
                if (curpage == 2) {
                     $('#restore-dialog').dialog('close');
                } else if (curpage == 1) {

                    var restorePath = null;

                    var overwrite = $('#restore-overwrite-overwrite').is(':checked');

                    var restore_permissions = $('#restore-metadata-permissions').is(':checked');
                    var skip_metadata = $('#restore-metadata-none').is(':checked');

                    if ($('#restore-overwrite-target-other').is(':checked'))
                        restorePath = $('#restore-target-path').val();

                    var tasks = [];
                    for(var n in includeMap) {
                        var t = {
                            'action': 'restore-files',
                            time: $('#restore-version').val(),
                            'HTTP_METHOD': 'POST',
                            id: backupId,
                            'restore-path': restorePath,
                            'overwrite': overwrite,
                            'permissions': restore_permissions,
                            'skip-metadata': skip_metadata,
                            paths: []
                        };
                        for(var p in includeMap[n]) {
                            if (p.lastIndexOf(dirSep) == p.length -1)
                                t.paths.push(p + '*');
                            else
                                t.paths.push(p);
                        }

                        if (t.paths.length > 0)
                        {
                            t.paths = t.paths.join(pathSep);
                            tasks.push(t);
                        }
                    }

                    if (isDirectRestore) {

                        var backupid = null;

                        var dlg = APP_UTIL.create_modal_task_wait('Creating database', 'Building local database ... ', function(cancelled, taskid) {
                            if (!cancelled)
                                performRestore(tasks);
                        });

                        APP_DATA.callServer({ action: 'copy-backup-to-temp', 'id': backupId }, function(data) {
                            var bkid = data.ID;
                            for(var n in tasks)
                                tasks[n].id = bkid;

                            APP_DATA.callServer({ action: 'send-command', command: 'run-repair', 'id': bkid, 'time':  $('#restore-version').val(), 'paths': tasks[0].paths, 'HTTP_METHOD': 'POST' }, function(data) {
                                dlg.register_updates(data.ID);
                            }, function(a,b,msg) {
                                alert('Failed to connect: ' + msg);
                                dlg.dialog('close');
                                dlg.remove();
                            });                            
                        }, function(a,b,msg) {
                            alert('Failed to connect: ' + msg);
                            dlg.dialog('close');
                            dlg.remove();
                        });                        


                    } else {
                        performRestore(tasks);
                    }
                } else {
                    var els = 0;
                    includeMap = buildIncludeMap();

                    for(var t in includeMap)
                        for(var i in includeMap[t])
                            els++;

                    if (els == 0) {
                        alert('You must select at least one path to restore');
                        return;
                    }

                    curpage = Math.min(2, curpage+1);
                    updatePageNav();

                }
            }}
        ]        
    });

    var dlg_buttons = $('#restore-dialog').parent().find('.ui-dialog-buttonpane').find('.ui-button');
    var updatePageNav = function() {
        if (curpage == 0) {
            $('#restore-files-page').show();
            $('#restore-path-page').hide();
            $('#restore-complete-page').hide();
            dlg_buttons.first().show();
            dlg_buttons.last().button('option', 'label', 'Next >');
        } else if (curpage == 1) {
            $('#restore-files-page').hide();
            $('#restore-path-page').show();
            $('#restore-complete-page').hide();
            dlg_buttons.first().show();
            dlg_buttons.last().button('option', 'label', 'Restore');
        } else {
            $('#restore-files-page').hide();
            $('#restore-path-page').hide();
            $('#restore-complete-page').show();

            dlg_buttons.first().hide();
            dlg_buttons.last().button('option', 'label', 'OK');
        }

        dlg_buttons.first().button('option', 'disabled', curpage == 0);
    };

    $('#restore-search').watermark('Search for files & folders');

    $('#restore-files-page').show();
    $('#restore-path-page').hide();
    $('#restore-complete-page').hide();

    var colorize = function(tree, term, entry) {
        var tr = tree.jstree();
        $(entry || tree).find('a.jstree-anchor').each(function(i, e) {
            var s = $(e);
            var node = tr.get_node(s);
            if (node.text) {
                s.children('.search-match').remove();
                var o = s.children();
                s.html(replace_all_insensitive(node.text, term, '<div class="search-match">$1</div>'));
                s.prepend(o);
            }
        });
    };

    var buildSearchNodes = function(time, data, search) {
        var rootpath = commonPrefix[time];
        if (rootpath != '' && rootpath[rootpath.length - 1] != dirSep)
            rootpath += dirSep;
        var rootnode = createNodeFromData(commonPrefix[time], commonPrefix[time], time, true);
        rootnode.state = rootnode.state || {};
        rootnode.state.opened = true;


        for(var i = 0; i < data.Files.length; i++) {
            var n = rootnode;
            var p = rootpath;
            var sp = data.Files[i].Path;

            if (search && sp.toLowerCase().indexOf(search.toLowerCase()) < 0)
                continue;

            var isDir = sp.substr(sp.length - 1) == dirSep;
            if (isDir)
                sp = sp.substr(0, sp.length - 1);

            var items = sp.substr(rootpath.length).split(dirSep);

            for(var j = 0; j < items.length; j++) {
                p += items[j] + (!isDir && j == items.length - 1 ? '' : dirSep);
                
                var e = null;
                for(var x in n.children)
                    if (n.children[x].filepath == p) {
                        e = n.children[x];
                        break;
                    }

                if (e == null) {
                    e = createNodeFromData(p, commonPrefix[time], time, false);
                    
                    if (j != items.length - 1) {
                        e.state = e.state || {};
                        e.state.opened = true;
                    }

                    if (n.children === false || n.children === true)
                        n.children = [];
                    n.children.push(e);

                }
                n = e;
            }
        }

        return [rootnode];
    }

    var inSearch = false;
    var doSearch = function(time, search) {
        if (inSearch)
            return;

        $('#restore-search-loader').show();
        inSearch = true;

        var stree = searchTrees[time];
        var tree = trees[time];

        var buildSearchTree = function(nodes) {
            var streeel = $('<div></div>');
            searchTrees[time] = streeel;
            $('#restore-files-tree').append(streeel);

            var te = streeel.jstree({
                'plugins' : [ 'checkbox' ],
                'core': { 'data' : nodes }
            });

            colorize(streeel, search);
            streeel.show();
        };

        if (searchdata[time]) {
            var t = searchdata[time];
            for(var p in t) {
                if (search.indexOf(p) == 0) {
                    colorize(tree, search);

                    if (t[p].Files.length == 0) {
                        alert('No results matched the query');
                        tree.show();
                    } else if (stree) {
                        colorize(stree, search);
                        tree.hide();
                        stree.show();
                    } else {
                        buildSearchTree(buildSearchNodes(time, t[p], search));
                        tree.hide();
                    }
                    $('#restore-search-loader').hide();
                    inSearch = false;
                    return;
                }
            }
        }

        if (tree) {
            colorize(tree, search);
            tree.hide();
        }
        if (stree)
            colorize(stree, search);


        if (stree) {
            stree.remove();
            delete searchTrees[time];
        }

        $.ajax({
            'url': APP_CONFIG.server_url,
            headers: {'X-XSRF-Token': APP_DATA.xsrf_token},
            'data': {
                'action': 'search-backup-files',
                'id': backupId,
                'time': time,
                'prefix-only': 'false',
                'filter': '*' + search + '*'
            },
            'dataType': 'json'
        })
        .done(function(data, status, xhr) {
            
            if (!searchdata[time])
                searchdata[time] = {};

            if (!manualIncludes[time])
                manualIncludes[time] = [];

            searchdata[time][search] = data;

            $('#restore-search-loader').hide();
            inSearch = false;

            if (data.Files.length == 0) {
                alert('No results matched the query');
                tree.show();
                return;
            }

            buildSearchTree(buildSearchNodes(time, data));

        })
        .fail(function() {
            $('#restore-search-loader').hide();
            inSearch = false;
            alert('Search failed ...');
            tree.show();
        });
    };

    var createNodeFromData = function(path, prefix, time, rootnode) {
        var disp = path.substr(prefix.length);
        var isFolder = disp.lastIndexOf(dirSep) == disp.length - 1;
        var icon = null;
        var state = null;
        if (isFolder)
            disp = disp.substr(0, disp.length - 1);
        else
            icon = 'icon-file icon-file-' + disp.substr(disp.lastIndexOf('.')+1);

        if (rootnode) {
            disp = APP_DATA.getBackupName(backupId) || disp;
            //state = {opened: true};
        }

        return {
            text: disp,
            filepath: path,
            time: time,
            children: isFolder,
            state: state,
            icon: icon
        };
    };

    var loadNodes = function(node, callback, time) {
        $.ajax({
            'url': APP_CONFIG.server_url,
            'data': {
                'action': 'search-backup-files',
                'id': backupId,
                'time': time,
                'prefix-only': node.id === '#',
                'folder-contents': node.id !== '#',
                'filter': node.id === '#' ? '*' : node.original.filepath,
                'Prefix': node.id === '#' ? '' : node.original.filepath
            },
            headers: {'X-XSRF-Token': APP_DATA.xsrf_token},
            'dataType': 'json'
        })
        .done(function(data, status, xhr) {
            var nodes = [];
            data.Files = data.Files || [];
            for(var i = 0; i < data.Files.length; i++)
                nodes.push(createNodeFromData(data.Files[i].Path, data.Prefix, time, node.id === '#'));

            callback(nodes, data);

            if (node.id === '#')
                commonPrefix[time] = data.Files[0].Path;
        });
    };

    var setupTree = function(time) {
        var treeel = trees[time];

        if (!trees[time]) {
            var treeel = $('<div></div>');
            trees[time] = treeel;
            $('#restore-files-tree').append(treeel);

            treeel.jstree({
                'core': {
                    'data': function(node, callback) {
                        if (nodedata[node.id]) {
                            callback(nodedata[node.id]);
                            colorize(treeel, $('#restore-search').val());
                        } else {
                            loadNodes(node, function(nodes, data) { 
                                callback(nodes);
                                if (data.Prefix == '')
                                    treeel.jstree("open_node", treeel.find('li').first());
                                colorize(treeel, $('#restore-search').val());
                            }, 
                            time); 
                        }
                    }
                },
                'plugins' : [ 'checkbox' ]
            });
        }

        for(var t in trees)
            if (t != time)
                trees[t].hide();
            else {
                trees[t].show();
            }
    };

    var buildIncludeMap = function() {
        var time = $('#restore-version').val();
        var m = {};
        //strees = strees || trees;
        var strees = [ trees[time] ];
        var partialfolders = false;
        if (searchTrees[time] && searchTrees[time].is(':visible')) {
            partialfolders = true;
            strees = [ searchTrees[time] ];
        }

        for(var t in strees) {
            m[t] = {};
            var tr = strees[t].jstree();

            var roots = strees[t].children('ul').children('li');
            var follow = [];

            roots.each(function(i,e) {
                follow.push(tr.get_node(e));
            });

            while(follow.length > 0) {
                var n = follow.pop();
                if (n.state.selected && n.original.filepath) {
                    // If we have partial folder matches, 
                    // we only include the leaves
                    if (partialfolders) {
                        if (!n.children || n.children.length == 0) {
                            m[t][n.original.filepath] = 1;
                        } else {
                            // Non-leaf node, recurse it
                            for(var c in n.children)
                                follow.push(tr.get_node(n.children[c]));                            
                        }

                    } else {
                        m[t][n.original.filepath] = 1;
                    }
                } else {
                    //TODO: This traverses the entire tree,
                    // we could choose only those with indeterminate,
                    // but that state only exists in the DOM which is
                    // removed when collapsed
                    for(var c in n.children)
                        follow.push(tr.get_node(n.children[c]));
                }
            }
        }

        return m;
    };

    $('#restore-dialog').on('setup-dialog', function(e, data) {
        backupId = data.id;
        isDirectRestore = data.isDirectRestore;
        trees = { };
        searchdata = { };
        dbversions = { };

        $('#restore-files-tree').empty();
        $('#restore-search-loader').hide();
        $('#restore-form').each(function(i, e) { e.reset(); });
        $('#restore-overwrite-overwrite').each(function(i, e) { e.checked = true; });
        $('#restore-overwrite-target-original').each(function(i, e) { e.checked = true; });
        $('#restore-metadata-timestamps').each(function(i, e) { e.checked = true; });

        curpage = 0; 
        updatePageNav();

        APP_DATA.getServerConfig(function(serverdata) {

            // TODO: Should be the backup data versions, not the host OS
            dirSep = serverdata.DirectorySeparator;
            pathSep = serverdata.PathSeparator;

            APP_DATA.callServer({ 'action': 'list-backup-sets', id: backupId, 'from-remote-only': isDirectRestore }, function(data, success, message) {
                    $('#restore-version').empty();

                    if (data == null || data.length == 0) {
                        alert('Failed to get list of backup times');
                        $('#restore-dialog').dialog('close');
                    }

                    for(var n in data)
                        dbversions[data[n].Time] = isDirectRestore ? n : true;
                    dbversions[data[0].Time] = true;

                    var latest_group = $('<optgroup></optgroup>').attr('label', 'Newest - ' + $.timeago(data[0].Time));
                    latest_group.append($("<option></option>").attr("value", data[0].Time).text($.toDisplayDateAndTime($.parseDate(data[0].Time))));
                    $('#restore-version').append(latest_group);

                    var dateStamp = function(a) { return a.getFullYear() * 10000 + a.getMonth() * 100 + a.getDate(); }
                    var now = new Date();
                    var today = dateStamp(now);
                    var yesterday = dateStamp(new Date(new Date().setDate(now.getDate() - 1)));
                    var week = dateStamp(new Date(new Date().setDate(now.getDate() - 7)));
                    var thismonth = dateStamp(new Date(new Date().setMonth(now.getMonth() - 1)));
                    var lastmonth = dateStamp(new Date(new Date().setMonth(now.getMonth() - 2)));

                    var dateBuckets = [
                        {text:'Today', stamp: today, items: []}, 
                        {text: 'Yesterday', stamp: yesterday, items: []}, 
                        {text: 'This week', stamp: week, items: []}, 
                        {text: 'This month', stamp: thismonth, items: []},
                        {text: 'Last month', stamp: lastmonth, items: []}
                    ];

                    var yearBuckets = { };

                    for(var i in data) {
                        if (i == '0')
                            continue;
                        var dt = $.parseDate(data[i].Time);
                        var stamp = dateStamp(dt);
                        var inserted = false;

                        for(var t in dateBuckets) {
                            if (stamp >= dateBuckets[t].stamp) {
                                dateBuckets[t].items.push(data[i])
                                inserted = true;
                                break;
                            }
                        }

                        if (!inserted) {
                            var y = dt.getFullYear() + '';
                            if (yearBuckets[y] == null) {
                                yearBuckets[y] = {text: y, stamp: dateStamp(new Date(dt.getFullYear(), 0, 1)), items: []};
                                dateBuckets.push(yearBuckets[y]);
                            }
                            yearBuckets[y].items.push(data[i]);
                        }
                    }


                    for(var n in dateBuckets) {
                        var e = dateBuckets[n];
                        if (e.items.length == 0)
                            continue;

                        var group = $('<optgroup></optgroup>').attr('label', e.text);
                        for(var d in e.items)
                            group.append($("<option></option>").attr("value", e.items[d].Time).text($.toDisplayDateAndTime($.parseDate(e.items[d].Time))));

                        $('#restore-version').append(group);

                    }

                    $('#restore-version').trigger('change');

                }, function(data, success, message) {
                    alert('Failed to get list of backup times:\n' + message);
                    $('#restore-dialog').dialog('close');
                });
        }, function() {
            alert('Failed to get server config');
            $('#restore-dialog').dialog('close');
        });
    });

    var doQuickSearch = function(search) {
        var search = $('#restore-search').val();
        var time = $('#restore-version').val();
        if (searchdata[time]) {
            for(var k in searchdata[time]) {
                if (search.indexOf(k) == 0) {
                    colorize(trees[time], search);
                    return true;
                }
            }
        }

        return false;
    };

    $('#restore-search').keypress(function(e) {
        var time = $('#restore-version').val();
        if (e.which == 13 && $('#restore-search').val().trim() != '')
            doSearch(time, $('#restore-search').val());
        else if ($('#restore-search').val().trim() != '') {
            colorize(trees[time], $('#restore-search').val());
            if (searchTrees[time])
                colorize(searchTrees[time], $('#restore-search').val());

        }
    });

    $('#restore-search').keyup(function(e) {
        var time = $('#restore-version').val();
        colorize(trees[time], $('#restore-search').val());
        if (searchTrees[time])
            colorize(searchTrees[time], $('#restore-search').val());
    });

    $('#restore-search').change(function(e) {
        var time = $('#restore-version').val();
        colorize(trees[time], $('#restore-search').val());
        if (searchTrees[time])
            colorize(searchTrees[time], $('#restore-search').val());
    });

    $('#restore-search').on('search', function(e) {
        var time = $('#restore-version').val();
        if ($('#restore-search').val() == '') {
            trees[time].show();
            if (searchTrees[time]) {
                searchTrees[time].remove();
                delete searchTrees[time];
            }
            colorize(trees[time], $('#restore-search').val());
        } else
            doSearch(time, $('#restore-search').val());
    });

    $('#restore-version').change(function() {
        $('#restore-search').val('');

        var selectedversion = $('#restore-version').val();

        if (dbversions[selectedversion] !== true) {
            // Need to patch the db with data

            var dlg = APP_UTIL.create_modal_task_wait('Updating database', 'Updating local database ... ', function(cancelled, taskid) {
                if (!cancelled) {
                    dbversions[selectedversion] = true;
                    setupTree(selectedversion);
                }
            });

            APP_DATA.callServer({ action: 'send-command', command: 'run-repair-update', 'only-paths': true, 'id': backupId, 'time': selectedversion }, function(data) {
                dlg.register_updates(data.ID);
            }, function(a,b,msg) {
                alert('Failed to connect: ' + msg);
                dlg.dialog('close');
                dlg.remove();
            });           
        } else {
            setupTree(selectedversion);
        }
    });
    
    $('#restore-target-path').keypress(function() { $('#restore-overwrite-target-other').each(function(i,e) { e.checked = true; }); });
    $('#restore-target-path').change(function() { $('#restore-overwrite-target-other').each(function(i,e) { e.checked = true; }); });

    $('#restore-overwrite-target-other').click(function() { $('#restore-target-path-browse').trigger('click'); } );

    $('#restore-target-path-browse').click(function(e) {
        $.browseForFolder({
            title: 'Select restore folder',
            resolvePath: true,
            multiSelect: false,
            callback: function(path, display) {
                $('#restore-target-path').val(path);
                $('#restore-overwrite-target-other').each(function(i, e) { e.checked = true; });
            }
        });
    });

    $('#restore-direct-uri').watermark('webdavs://user:pass@example.com:995/backup?option=true');
    $('#restore-direct-encryption-password').watermark('Enter backup passphrase, if any');
    $('#restore-direct-advanced-options').watermark('Enter one option per line in command-line format, eg. --prefix=backup');

    $('#restore-direct-uri-label').click(function() {
        $('#connection-uri-dialog').dialog('open');
        $('#connection-uri-dialog').trigger('setup-dialog', $('#restore-direct-uri'));
    });

    $('#restore-direct-dialog').dialog({
        minWidth: 320, 
        width: $('body').width > 600 ? 320 : 600, 
        minHeight: 480, 
        height: 500, 
        modal: true,
        autoOpen: false,
        closeOnEscape: true,
        buttons: [
            { text: 'Cancel', click: function(event, ui) {
                $(this).dialog('close');
            }},
            { text: 'Connect', click: function(event, ui) {
                var uri = $('#restore-direct-uri').val();
                var passphrase = $('#restore-direct-encryption-password').val();

                if (uri == null || uri.trim().length == 1) {
                    alert('You must supply a connection URI');
                    return;
                }

                var options = {};
                if (passphrase != null && passphrase.trim().length > 0)
                    options['--passphrase'] = passphrase;
                else
                    options['--no-encryption'] = 'true';

                if (!EDIT_URI.parse_extra_options($('#restore-direct-advanced-options'), options))
                    return;

                var settings = [];
                for(var k in options)
                    settings.push({'Name': k, 'Value': options[k]});

                var item = {
                    'Schedule': null,
                    'Backup': {
                        'TargetURL': uri,
                        'Settings': settings
                    }
                };

                var self = this;
                var backupid = null;

                var dlg = APP_UTIL.create_modal_task_wait('Creating database', 'Building local database ... ', function(cancelled, taskid) {
                    if (!cancelled) {
                        APP_DATA.restoreBackup(backupid, true);
                        $(self).dialog('close');
                    }
                });

                APP_DATA.testConnection(uri, function() {
                    APP_DATA.addBackup(item, function(data) {
                        backupid = data.ID;

                        APP_DATA.callServer({ action: 'send-command', command: 'run-repair', 'only-paths': true, 'id': backupid, 'version': 0 }, function(data) {
                            dlg.register_updates(data.ID);

                        }, function(a,b,msg) {
                            alert('Failed to connect: ' + msg);
                            dlg.dialog('close');
                            dlg.remove();
                        });                            
                    }, function(a,b,msg) {
                        alert('Failed to create a backup set: ' + msg);
                        dlg.dialog('close');
                        dlg.remove();
                    }, {'temporary': true});
                }, function(a,b,msg) {
                    alert('Failed to connect: ' + msg);
                    dlg.dialog('close');
                    dlg.remove();
                });

            }}
        ]
    });

    $('#restore-direct-options-dialog').dialog({
        minWidth: 320,
        width: $('body').width > 600 ? 320 : 600,
        minHeight: 480,
        height: 500,
        modal: true,
        autoOpen: false,
        closeOnEscape: true,
        buttons: [
            { text: 'Close', disabled: false, click: function(event, ui) {
                $(this).dialog('close');
            }}
        ]
    });

    $('#restore-direct-options-link').click(function() {
        APP_DATA.getServerConfig(function(data) {
            $('#restore-direct-options-dialog').dialog('open');

            var baseOpts = data.Options;

            for(var n in data.BackendModules)
                baseOpts = baseOpts.concat(data.BackendModules[n].Options);

            for(var n in data.CompressionModules)
                baseOpts = baseOpts.concat(data.CompressionModules[n].Options);

            for(var n in data.EncryptionModules)
                baseOpts = baseOpts.concat(data.EncryptionModules[n].Options);

            for(var n in data.GenericModules)
                baseOpts = baseOpts.concat(data.GenericModules[n].Options);


            $('#restore-direct-options-dialog').trigger('configure', { Options: baseOpts, callback: function(id) {
                $('#restore-direct-options-dialog').dialog('close');

                var txt = $('#restore-direct-advanced-options').val().trim();
                if (txt.length > 0)
                    txt += '\n';

                var defaultvalue = '';
                for(var o in data.Options)
                    if (data.Options[o].Name == id) {
                        defaultvalue = data.Options[o].DefaultValue;
                        break;
                    }


                txt += '--' + id + '=' + defaultvalue;
                $('#restore-direct-advanced-options').val('').val(txt);
                $('#restore-direct-advanced-options').focus();

            }});
        }, function() {
        });
    });    

    $('#restore-direct-options-dialog').on('configure', function(e, data) {
        $('#restore-direct-options-dialog').empty();

        var s = data.Options.sort(function(a, b){
            if (a == null)
                return -1;
            if (b == null)
                return 1;
            if (a == null && b == null)
                return 0;

            if(a.Name < b.Name) return -1;
            if(a.Name > b.Name) return 1;
            return 0;
        });

        //Fill with jQuery template
        $.tmpl($('#backup-option-template'), s).prependTo($('#restore-direct-options-dialog'));
        $('#restore-direct-options-dialog').find('.backup-option-link').click(function(e) {
            data.callback(e.target.id);
        });

        $('#restore-direct-options-dialog').find('.backup-option-long').each(function(i, e) {
            $(e).html(nl2br($(e).html()));
        });
    });    

   
});