summaryrefslogtreecommitdiffstats
path: root/deluge/ui/web/js/deluge-all/TorrentGrid.js
blob: 2a468f0bc58e360c9e8af19029a6324c125aab6a (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
/*!
 * Deluge.TorrentGrid.js
 *
 * Copyright (c) Damien Churchill 2009-2011 <damoxc@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to:
 *     The Free Software Foundation, Inc.,
 *     51 Franklin Street, Fifth Floor
 *     Boston, MA  02110-1301, USA.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the OpenSSL
 * library.
 * You must obey the GNU General Public License in all respects for all of
 * the code used other than OpenSSL. If you modify file(s) with this
 * exception, you may extend this exception to your version of the file(s),
 * but you are not obligated to do so. If you do not wish to do so, delete
 * this exception statement from your version. If you delete this exception
 * statement from all source files in the program, then also delete it here.
 */

/* Renderers for the Torrent Grid */
function queueRenderer(value) {
    return (value == -1) ? '' : value + 1;
}
function torrentNameRenderer(value, p, r) {
    return Ext.String.format('<div class="torrent-name x-deluge-{0}">{1}</div>', r.data['state'].toLowerCase(), value);
}
function torrentSpeedRenderer(value) {
    if (!value) return;
    return fspeed(value);
}
function torrentProgressRenderer(value, md, r) {
    value = new Number(value);
    var width = this.query('gridcolumn[dataIndex=progress]')[0].getWidth(),
        progress = value,
        text = r.data['state'] + ' ' + value.toFixed(2) + '%';
    return Deluge.progressBar(value, width - 8, text);
}
function seedsRenderer(value, p, r) {
    if (r.data['total_seeds'] > -1) {
        return Ext.String.format('{0} ({1})', value, r.data['total_seeds']);
    } else {
        return value;
    }
}
function peersRenderer(value, p, r) {
    if (r.data['total_peers'] > -1) {
        return Ext.String.format('{0} ({1})', value, r.data['total_peers']);
    } else {
        return value;
    }
}
function availRenderer(value, p, r)    {
    return (value < 0) ? '&infin;' : new Number(value).toFixed(3);
}
function trackerRenderer(value, p, r) {
    return Ext.String.format('<div style="background: url(' + deluge.config.base + 'tracker/{0}) no-repeat; padding-left: 20px;">{0}</div>', value);
}

function etaSorter(eta) {
    return eta * -1;
}

function dateOrNever(date) {
    return date > 0.0 ? fdate(date) : "Never"
}

/**
 * Deluge.TorrentGrid Class
 *
 * @author Damien Churchill <damoxc@gmail.com>
 * @version 1.3
 *
 * @class Deluge.TorrentGrid
 * @extends Ext.grid.Panel
 * @constructor
 * @param {Object} config Configuration options
 */
Ext.define('Deluge.TorrentGrid', {
    extend: 'Ext.grid.Panel',

    // object to store contained torrent ids
    torrents: {},

    columns: [{
        id:'queue',
        header: _('#'),
        width: 30,
        sortable: true,
        renderer: queueRenderer,
        dataIndex: 'queue'
    }, {
        id:'name',
        header: _('Name'),
        width: 150,
        sortable: true,
        renderer: torrentNameRenderer,
        dataIndex: 'name'
    }, {
        header: _('Size'),
        width: 75,
        sortable: true,
        renderer: fsize,
        dataIndex: 'total_size'
    }, {
        header: _('Progress'),
        width: 150,
        sortable: true,
        renderer: torrentProgressRenderer,
        dataIndex: 'progress'
    }, {
        header: _('Seeders'),
        width: 60,
        sortable: true,
        renderer: seedsRenderer,
        dataIndex: 'num_seeds'
    }, {
        header: _('Peers'),
        width: 60,
        sortable: true,
        renderer: peersRenderer,
        dataIndex: 'num_peers'
    }, {
        header: _('Down Speed'),
        width: 80,
        sortable: true,
        renderer: torrentSpeedRenderer,
        dataIndex: 'download_payload_rate'
    }, {
        header: _('Up Speed'),
        width: 80,
        sortable: true,
        renderer: torrentSpeedRenderer,
        dataIndex: 'upload_payload_rate'
    }, {
        header: _('ETA'),
        width: 60,
        sortable: true,
        renderer: ftime,
        dataIndex: 'eta'
    }, {
        header: _('Ratio'),
        width: 60,
        sortable: true,
        renderer: availRenderer,
        dataIndex: 'ratio'
    }, {
        header: _('Avail'),
        width: 60,
        sortable: true,
        renderer: availRenderer,
        dataIndex: 'distributed_copies'
    }, {
        header: _('Added'),
        width: 80,
        sortable: true,
        renderer: fdate,
        dataIndex: 'time_added'
    }, {
        header: _('Last Seen Complete'),
        width: 80,
        sortable: true,
        renderer: dateOrNever,
        dataIndex: 'last_seen_complete'
    }, {
        header: _('Tracker'),
        width: 120,
        sortable: true,
        renderer: trackerRenderer,
        dataIndex: 'tracker_host'
    }, {
        header: _('Save Path'),
        width: 120,
        sortable: true,
        renderer: fplain,
        dataIndex: 'save_path'
    }, {
        header: _('Owner'),
        width: 80,
        sortable: true,
        renderer: fplain,
        dataIndex: 'owner'
    }, {
        header: _('Public'),
        width: 80,
        sortable: true,
        renderer: fplain,
        dataIndex: 'public'
    }, {
        header: _('Shared'),
        width: 80,
        sortable: true,
        renderer: fplain,
        dataIndex: 'shared'
    }],

    meta: {
        root: 'torrents',
        idProperty: 'id',
        fields: [
            {name: 'queue', sortType: Deluge.data.SortTypes.asQueuePosition},
            {name: 'name'},
            {name: 'total_size', type: 'int'},
            {name: 'state'},
            {name: 'progress', type: 'float'},
            {name: 'num_seeds', type: 'int'},
            {name: 'total_seeds', type: 'int'},
            {name: 'num_peers', type: 'int'},
            {name: 'total_peers', type: 'int'},
            {name: 'download_payload_rate', type: 'int'},
            {name: 'upload_payload_speed', type: 'int'},
            {name: 'eta', type: 'int', sortType: etaSorter},
            {name: 'ratio', type: 'float'},
            {name: 'distributed_copies', type: 'float'},
            {name: 'time_added', type: 'int'},
            {name: 'tracker_host'},
            {name: 'save_path'}
        ]
    },

    store: Ext.create('Ext.data.Store', {
        model: 'Deluge.data.Torrent',
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'torrents'
            }
        }
    }),

    id: 'torrentGrid',
    region: 'center',
    cls: 'deluge-torrents',
    stripeRows: true,
    autoExpandColumn: 'name',
    deferredRender:false,
    autoScroll:true,
    margins: '5 5 0 0',
    stateful: true,

    initComponent: function() {
        this.callParent(arguments);
        deluge.events.on('torrentRemoved', this.onTorrentRemoved, this);
        deluge.events.on('disconnect', this.onDisconnect, this);

        this.on('itemcontextmenu', this.onTorrentSelected, this);
    },

    /**
     * Returns the record representing the torrent at the specified index.
     *
     * @param index {int} The row index of the torrent you wish to retrieve.
     * @return {Ext.data.Record} The record representing the torrent.
     */
    getTorrent: function(index) {
        return this.getStore().getAt(index);
    },

    /**
     * Returns the currently selected record.
     * @ return {Array/Ext.data.Record} The record(s) representing the rows
     */
    getSelected: function() {
        return this.getSelectionModel().getLastSelected();
    },

    /**
     * Returns the currently selected records.
     */
    getSelections: function() {
        return this.getSelectionModel().getSelection();
    },

    /**
     * Return the currently selected torrent id.
     * @return {String} The currently selected id.
     */
    getSelectedId: function() {
        return this.getSelected().getId()
    },

    /**
     * Return the currently selected torrent ids.
     * @return {Array} The currently selected ids.
     */
    getSelectedIds: function() {
        var ids = [];
        Ext.each(this.getSelections(), function(r) {
            ids.push(r.getId());
        });
        return ids;
    },

    update: function(torrents, wipe) {
        var store = this.getStore();

        // Need to perform a complete reload of the torrent grid.
        if (wipe) {
            store.removeAll();
            this.torrents = {};
        }

        var newTorrents = [];

        // Update and add any new torrents.
        for (var t in torrents) {
            var torrent = torrents[t];

            if (this.torrents[t]) {
                var record = store.getById(t);
                record.beginEdit();
                for (var k in torrent) {
                    if (record.get(k) != torrent[k]) {
                        record.set(k, torrent[k]);
                    }
                }
                record.endEdit();
            } else {
                var record = new Deluge.data.Torrent(torrent);
                record.setId(t);
                this.torrents[t] = 1;
                newTorrents.push(record);
            }
        }
        store.add(newTorrents);

        // Remove any torrents that should not be in the store.
        store.each(function(record) {
            if (!torrents[record.getId()]) {
                store.remove(record);
                delete this.torrents[record.getId()];
            }
        }, this);
        store.sync();

        // TODO: re-enable this is it's required.
        //store.sort(store.sorters);
    },

    // private
    onDisconnect: function() {
        this.getStore().removeAll();
        this.torrents = {};
    },

    // private
    onTorrentRemoved: function(torrentIds) {
        var selModel = this.getSelectionModel();
        Ext.each(torrentIds, function(torrentId) {
            var record = this.getStore().getById(torrentId);
            if (selModel.isSelected(record)) {
                selModel.deselectRow(this.getStore().indexOf(record));
            }
            this.getStore().remove(record);
            delete this.torrents[torrentId];
        }, this);
    },

    onTorrentSelected: function(grid, record, item, i, e, opts) {
        e.stopEvent();
        var sm = grid.getSelectionModel();
        if (!sm.hasSelection()) {
            sm.select(record);
        }
        var point = e.getPoint();
        deluge.menus.torrent.showAt(point.x, point.y);
    }
});