summaryrefslogtreecommitdiffstats
path: root/deluge/plugins/Scheduler/deluge_scheduler/data/scheduler.js
blob: f59068c5ad1021790ac80df7a40364aa75903df3 (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
/**
 * scheduler.js
 *     The client-side javascript code for the Scheduler plugin.
 *
 * Copyright (C) samuel337 2011
 *
 * This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
 * the additional special exception to link portions of this program with the OpenSSL library.
 * See LICENSE for more details.
 *
 */

Ext.ns('Deluge.ux');

Deluge.ux.ScheduleSelector = Ext.extend(Ext.form.FieldSet, {
    title: _('Schedule'),
    autoHeight: true,
    style: 'margin-bottom: 0px; padding-bottom: 0px;',
    border: false,

    states: [
        {
            name: 'Normal',
            backgroundColor: 'LightGreen',
            borderColor: 'DarkGreen',
            value: 0,
        },
        {
            name: 'Throttled',
            backgroundColor: 'Yellow',
            borderColor: 'Gold',
            value: 1,
        },
        {
            name: 'Paused',
            backgroundColor: 'OrangeRed',
            borderColor: 'FireBrick',
            value: 2,
        },
    ],
    daysOfWeek: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],

    initComponent: function () {
        Deluge.ux.ScheduleSelector.superclass.initComponent.call(this);

        // ExtJS' radiogroup implementation is very broken for styling.
        /*this.stateBrush = this.add({
            xtype: 'radiogroup',
            fieldLabel: _('State Brush'),
            name: 'current_state_brush',
            submitValue: false,
            items: [
                { boxLabel: 'Normal', name: 'current_state_brush', inputValue: 0 },
                { boxLabel: 'Throttled', name: 'current_state_brush', inputValue: 1, checked: true },
                { boxLabel: 'Paused', name: 'current_state_brush', inputValue: 2 },
            ]
        });*/
    },

    onRender: function (ct, position) {
        Deluge.ux.ScheduleSelector.superclass.onRender.call(this, ct, position);

        var dom = this.body.dom;

        function createEl(parent, type) {
            var el = document.createElement(type);
            parent.appendChild(el);
            return el;
        }

        // create state brushes
        // tack a random number to the end to avoid clashes
        this.stateBrushName =
            'schedule-state-brush-' + Math.round(Math.random() * 10000);

        var el1 = createEl(dom, 'div');

        var el2 = createEl(el1, 'div');
        this.stateBrush = el2;
        el2.id = this.stateBrushName;

        // for webkit
        var floatAttr = 'float';
        if (el2.style.float == undefined) {
            // for firefox
            if (el2.style.cssFloat != undefined) floatAttr = 'cssFloat';
            // for IE
            if (el2.style.styleFloat != undefined) floatAttr = 'styleFloat';
        }
        el2.style[floatAttr] = 'right';

        for (var i = 0; i < this.states.length; i++) {
            var el3 = createEl(el2, 'input');
            el3.type = 'radio';
            el3.value = this.states[i].value;
            el3.name = this.stateBrushName;
            el3.id = this.stateBrushName + '-' + this.states[i].name;

            // isn't the first one
            if (i > 0) el3.style.marginLeft = '7px';

            // assume the first is the default state, so make the 2nd one the default brush
            if (i == 1) el3.checked = true;

            var el4 = createEl(el2, 'label');
            el4.appendChild(document.createTextNode(this.states[i].name));
            el4.htmlFor = el3.id;
            el4.style.backgroundColor = this.states[i].backgroundColor;
            el4.style.borderBottom = '2px solid ' + this.states[i].borderColor;
            el4.style.padding = '2px 3px';
            el4.style.marginLeft = '3px';
        }

        el1.appendChild(document.createTextNode('Select a state brush:'));

        el1.style.marginBottom = '10px';

        // keep the radio buttons separate from the time bars
        createEl(dom, 'div').style.clear = 'both';

        var table = createEl(dom, 'table');
        table.cellSpacing = 0;

        // cache access to cells for easier access later
        this.scheduleCells = {};

        Ext.each(
            this.daysOfWeek,
            function (day) {
                var cells = [];
                var row = createEl(table, 'tr');
                var label = createEl(row, 'th');
                label.setAttribute(
                    'style',
                    'font-weight: bold; padding-right: 5px;'
                );
                label.appendChild(document.createTextNode(day));
                for (var hour = 0; hour < 24; hour++) {
                    var cell = createEl(row, 'td');

                    // assume the first state is the default state
                    cell.currentValue = cell.oldValue = this.states[0].value;
                    cell.day = day;
                    cell.hour = hour;

                    cell.width = '16px';
                    cell.height = '20px';

                    cell.style.border = '1px solid #999999';
                    // don't repeat borders in between cells
                    if (hour != 23)
                        // not the last cell
                        cell.style.borderRight = 'none';

                    this.updateCell(cell);

                    cells.push(cell);

                    cell = Ext.get(cell);
                    cell.on('click', this.onCellClick, this);
                    cell.on('mouseover', this.onCellMouseOver, this);
                    cell.on('mouseout', this.onCellMouseOut, this);
                    cell.on('mousedown', this.onCellMouseDown, this);
                    cell.on('mouseup', this.onCellMouseUp, this);
                }

                // insert gap row to provide visual separation
                row = createEl(table, 'tr');
                // blank cell to create gap
                createEl(row, 'td').height = '3px';

                this.scheduleCells[day] = cells;
            },
            this
        );
    },

    updateCell: function (cell) {
        // sanity check
        if (cell.currentValue == undefined) return;

        for (var i in this.states) {
            var curState = this.states[i];
            if (curState.value == cell.currentValue) {
                cell.style.background = curState.backgroundColor;
                break;
            }
        }
    },

    getCurrentBrushValue: function () {
        var v = null;
        var brushes = Ext.get(this.body.dom).findParent('form').elements[
            this.stateBrushName
        ];
        Ext.each(brushes, function (b) {
            if (b.checked) v = b.value;
        });

        return v;
    },

    onCellClick: function (event, cell) {
        cell.oldValue = cell.currentValue;

        this.dragAnchor = null;
    },

    onCellMouseDown: function (event, cell) {
        this.dragAnchor = cell;
    },

    onCellMouseUp: function (event, cell) {
        // if we're dragging...
        if (this.dragAnchor) {
            // set all those between here and the anchor to the new values
            if (cell.hour > this.dragAnchor.hour)
                this.confirmCells(cell.day, this.dragAnchor.hour, cell.hour);
            else if (cell.hour < this.dragAnchor.hour)
                this.confirmCells(cell.day, cell.hour, this.dragAnchor.hour);
            else this.confirmCells(cell.day, cell.hour, cell.hour);

            this.hideCellLeftTooltip();
            this.hideCellRightTooltip();
            this.dragAnchor = null;
        }
    },

    onCellMouseOver: function (event, cell) {
        // LEFT TOOL TIP
        // if it isn't showing and we're dragging, show it.
        // otherwise if dragging, leave it alone unless we're dragging to the left.
        // if we're not dragging, show it.
        var leftTooltipCell = null;
        if (!this.dragAnchor) leftTooltipCell = cell;
        else if (
            (this.dragAnchor && this.isCellLeftTooltipHidden()) ||
            (this.dragAnchor && this.dragAnchor.hour > cell.hour)
        )
            leftTooltipCell = this.dragAnchor;

        if (leftTooltipCell) {
            var hour = leftTooltipCell.hour;
            var pm = false;

            // convert to 12-hour time
            if (hour >= 12) {
                pm = true;
                if (hour > 12) hour -= 12;
            } else if (hour == 0) {
                // change 0 hour to 12am
                hour = 12;
            }
            this.showCellLeftTooltip(
                hour + ' ' + (pm ? 'pm' : 'am'),
                leftTooltipCell
            );
        }

        // RIGHT TOOL TIP
        var rightTooltipCell = null;
        if (this.dragAnchor) {
            if (this.dragAnchor.hour == cell.hour) this.hideCellRightTooltip();
            else if (
                this.dragAnchor.hour > cell.hour &&
                this.isCellRightTooltipHidden()
            )
                rightTooltipCell = this.dragAnchor;
            // cell.hour > this.dragAnchor.hour
            else rightTooltipCell = cell;
        }

        if (rightTooltipCell) {
            var hour = rightTooltipCell.hour;
            var pm = false;

            // convert to 12-hour time
            if (hour >= 12) {
                pm = true;
                if (hour > 12) hour -= 12;
            } else if (hour == 0) {
                // change 0 hour to 12am
                hour = 12;
            }
            this.showCellRightTooltip(
                hour + ' ' + (pm ? 'pm' : 'am'),
                rightTooltipCell
            );
        }

        // preview colour change and
        // revert state for all those on the outer side of the drag if dragging
        if (this.dragAnchor) {
            if (cell.day != this.dragAnchor.day) {
                // dragged into another day. Abort! Abort!
                Ext.each(
                    this.daysOfWeek,
                    function (day) {
                        this.revertCells(day, 0, 23);
                    },
                    this
                );
                this.dragAnchor = null;
                this.hideCellLeftTooltip();
                this.hideCellRightTooltip();
            } else if (cell.hour > this.dragAnchor.hour) {
                // dragging right
                this.revertCells(cell.day, cell.hour + 1, 23);
                this.previewCells(cell.day, this.dragAnchor.hour, cell.hour);
            } else if (cell.hour < this.dragAnchor.hour) {
                // dragging left
                this.revertCells(cell.day, 0, cell.hour - 1);
                this.previewCells(cell.day, cell.hour, this.dragAnchor.hour);
            } else {
                // back to anchor cell
                // don't know if it is from right or left, so revert all except this
                this.revertCells(cell.day, cell.hour + 1, 23);
                this.revertCells(cell.day, 0, cell.hour - 1);
            }
        } else {
            // not dragging, just preview this cell
            this.previewCells(cell.day, cell.hour, cell.hour);
        }
    },

    onCellMouseOut: function (event, cell) {
        if (!this.dragAnchor) this.hideCellLeftTooltip();

        // revert state. If new state has been set, old and new will be equal.
        // if dragging, this will be handled by the next mouse over
        if (this.dragAnchor == null && cell.oldValue != cell.currentValue) {
            this.revertCells(cell.day, cell.hour, cell.hour);
        }
    },

    previewCells: function (day, fromHour, toHour) {
        var cells = this.scheduleCells[day];
        var curBrushValue = this.getCurrentBrushValue();

        if (toHour > cells.length) toHour = cells.length;

        for (var i = fromHour; i <= toHour; i++) {
            if (cells[i].currentValue != curBrushValue) {
                cells[i].oldValue = cells[i].currentValue;
                cells[i].currentValue = curBrushValue;
                this.updateCell(cells[i]);
            }
        }
    },

    revertCells: function (day, fromHour, toHour) {
        var cells = this.scheduleCells[day];

        if (toHour > cells.length) toHour = cells.length;

        for (var i = fromHour; i <= toHour; i++) {
            cells[i].currentValue = cells[i].oldValue;
            this.updateCell(cells[i]);
        }
    },

    confirmCells: function (day, fromHour, toHour) {
        var cells = this.scheduleCells[day];

        if (toHour > cells.length) toHour = cells.length;

        for (var i = fromHour; i <= toHour; i++) {
            if (cells[i].currentValue != cells[i].oldValue) {
                cells[i].oldValue = cells[i].currentValue;
            }
        }
    },

    showCellLeftTooltip: function (text, cell) {
        var tooltip = this.cellLeftTooltip;

        if (!tooltip) {
            // no cached left tooltip exists, create one
            tooltip = document.createElement('div');
            this.cellLeftTooltip = tooltip;
            this.body.dom.appendChild(tooltip);
            tooltip.style.position = 'absolute';
            tooltip.style.backgroundColor = '#F2F2F2';
            tooltip.style.border = '1px solid #333333';
            tooltip.style.padding = '1px 3px';
            tooltip.style.opacity = 0.8;
        }

        // remove all existing children
        while (tooltip.childNodes.length > 0) {
            tooltip.removeChild(tooltip.firstChild);
        }
        // add the requested text
        tooltip.appendChild(document.createTextNode(text));

        // place the tooltip
        Ext.get(tooltip).alignTo(cell, 'br-tr');

        // make it visible
        tooltip.style.visibility = 'visible';
    },

    hideCellLeftTooltip: function () {
        if (this.cellLeftTooltip) {
            this.cellLeftTooltip.style.visibility = 'hidden';
        }
    },

    isCellLeftTooltipHidden: function () {
        if (this.cellLeftTooltip)
            return this.cellLeftTooltip.style.visibility == 'hidden';
        else return true;
    },

    showCellRightTooltip: function (text, cell) {
        var tooltip = this.cellRightTooltip;

        if (!tooltip) {
            // no cached left tooltip exists, create one
            tooltip = document.createElement('div');
            this.cellRightTooltip = tooltip;
            this.body.dom.appendChild(tooltip);
            tooltip.style.position = 'absolute';
            tooltip.style.backgroundColor = '#F2F2F2';
            tooltip.style.border = '1px solid #333333';
            tooltip.style.padding = '1px 3px';
            tooltip.style.opacity = 0.8;
        }

        // remove all existing children
        while (tooltip.childNodes.length > 0) {
            tooltip.removeChild(tooltip.firstChild);
        }
        // add the requested text
        tooltip.appendChild(document.createTextNode(text));

        // place the tooltip
        Ext.get(tooltip).alignTo(cell, 'bl-tl');

        // make it visible
        tooltip.style.visibility = 'visible';
    },

    hideCellRightTooltip: function () {
        if (this.cellRightTooltip) {
            this.cellRightTooltip.style.visibility = 'hidden';
        }
    },

    isCellRightTooltipHidden: function () {
        if (this.cellRightTooltip)
            return this.cellRightTooltip.style.visibility == 'hidden';
        else return true;
    },

    getConfig: function () {
        var config = [];

        for (var i = 0; i < 24; i++) {
            var hourConfig = [0, 0, 0, 0, 0, 0, 0];

            for (var j = 0; j < this.daysOfWeek.length; j++) {
                hourConfig[j] = parseInt(
                    this.scheduleCells[this.daysOfWeek[j]][i].currentValue
                );
            }

            config.push(hourConfig);
        }

        return config;
    },

    setConfig: function (config) {
        for (var i = 0; i < 24; i++) {
            var hourConfig = config[i];

            for (var j = 0; j < this.daysOfWeek.length; j++) {
                if (this.scheduleCells == undefined) {
                    var cell = hourConfig[j];
                } else {
                    var cell = this.scheduleCells[this.daysOfWeek[j]][i];
                }
                cell.currentValue = cell.oldValue = hourConfig[j];
                this.updateCell(cell);
            }
        }
    },
});

Ext.ns('Deluge.ux.preferences');

Deluge.ux.preferences.SchedulerPage = Ext.extend(Ext.Panel, {
    border: false,
    title: _('Scheduler'),
    header: false,
    layout: 'fit',

    initComponent: function () {
        Deluge.ux.preferences.SchedulerPage.superclass.initComponent.call(this);

        this.form = this.add({
            xtype: 'form',
            layout: 'form',
            border: false,
            autoHeight: true,
        });

        this.schedule = this.form.add(new Deluge.ux.ScheduleSelector());

        this.slowSettings = this.form.add({
            xtype: 'fieldset',
            border: false,
            title: _('Throttled Settings'),
            autoHeight: true,
            defaultType: 'spinnerfield',
            defaults: {
                minValue: -1,
                maxValue: 99999,
            },
            style: 'margin-top: 5px; margin-bottom: 0px; padding-bottom: 0px;',
            labelWidth: 200,
        });

        this.downloadLimit = this.slowSettings.add({
            fieldLabel: _('Maximum Download Speed (KiB/s)'),
            name: 'download_limit',
            width: 80,
            value: -1,
            decimalPrecision: 0,
        });
        this.uploadLimit = this.slowSettings.add({
            fieldLabel: _('Maximum Upload Speed (KiB/s)'),
            name: 'upload_limit',
            width: 80,
            value: -1,
            decimalPrecision: 0,
        });
        this.activeTorrents = this.slowSettings.add({
            fieldLabel: _('Active Torrents'),
            name: 'active_torrents',
            width: 80,
            value: -1,
            decimalPrecision: 0,
        });
        this.activeDownloading = this.slowSettings.add({
            fieldLabel: _('Active Downloading'),
            name: 'active_downloading',
            width: 80,
            value: -1,
            decimalPrecision: 0,
        });
        this.activeSeeding = this.slowSettings.add({
            fieldLabel: _('Active Seeding'),
            name: 'active_seeding',
            width: 80,
            value: -1,
            decimalPrecision: 0,
        });

        this.on('show', this.updateConfig, this);
    },

    onRender: function (ct, position) {
        Deluge.ux.preferences.SchedulerPage.superclass.onRender.call(
            this,
            ct,
            position
        );
        this.form.layout = new Ext.layout.FormLayout();
        this.form.layout.setContainer(this);
        this.form.doLayout();
    },

    onApply: function () {
        // build settings object
        var config = {};

        config['button_state'] = this.schedule.getConfig();
        config['low_down'] = this.downloadLimit.getValue();
        config['low_up'] = this.uploadLimit.getValue();
        config['low_active'] = this.activeTorrents.getValue();
        config['low_active_down'] = this.activeDownloading.getValue();
        config['low_active_up'] = this.activeSeeding.getValue();

        deluge.client.scheduler.set_config(config);
    },

    onOk: function () {
        this.onApply();
    },

    updateConfig: function () {
        deluge.client.scheduler.get_config({
            success: function (config) {
                this.schedule.setConfig(config['button_state']);
                this.downloadLimit.setValue(config['low_down']);
                this.uploadLimit.setValue(config['low_up']);
                this.activeTorrents.setValue(config['low_active']);
                this.activeDownloading.setValue(config['low_active_down']);
                this.activeSeeding.setValue(config['low_active_up']);
            },
            scope: this,
        });
    },
});

Deluge.plugins.SchedulerPlugin = Ext.extend(Deluge.Plugin, {
    name: 'Scheduler',

    onDisable: function () {
        deluge.preferences.removePage(this.prefsPage);
    },

    onEnable: function () {
        this.prefsPage = deluge.preferences.addPage(
            new Deluge.ux.preferences.SchedulerPage()
        );
    },
});
Deluge.registerPlugin('Scheduler', Deluge.plugins.SchedulerPlugin);