summaryrefslogtreecommitdiffstats
path: root/deluge/ui/web/js/deluge-all/details/FilesTab.js
blob: 3a212fa1e98f11c1e0f9bd29950bbf87bfe5e72f (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
/**
 * Deluge.details.FilesTab.js
 *
 * Copyright (c) Damien Churchill 2009-2010 <damoxc@gmail.com>
 *
 * 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.
 */

Deluge.details.FilesTab = Ext.extend(Ext.ux.tree.TreeGrid, {
    title: _('Files'),

    rootVisible: false,

    columns: [
        {
            header: _('Filename'),
            width: 330,
            dataIndex: 'filename',
        },
        {
            header: _('Size'),
            width: 150,
            dataIndex: 'size',
            tpl: new Ext.XTemplate('{size:this.fsize}', {
                fsize: function(v) {
                    return fsize(v);
                },
            }),
        },
        {
            xtype: 'tgrendercolumn',
            header: _('Progress'),
            width: 150,
            dataIndex: 'progress',
            renderer: function(v) {
                var progress = v * 100;
                return Deluge.progressBar(
                    progress,
                    this.col.width,
                    progress.toFixed(2) + '%',
                    0
                );
            },
        },
        {
            header: _('Priority'),
            width: 150,
            dataIndex: 'priority',
            tpl: new Ext.XTemplate(
                '<tpl if="!isNaN(priority)">' +
                    '<div class="{priority:this.getClass}">' +
                    '{priority:this.getName}' +
                    '</div></tpl>',
                {
                    getClass: function(v) {
                        return FILE_PRIORITY_CSS[v];
                    },

                    getName: function(v) {
                        return _(FILE_PRIORITY[v]);
                    },
                }
            ),
        },
    ],

    selModel: new Ext.tree.MultiSelectionModel(),

    initComponent: function() {
        Deluge.details.FilesTab.superclass.initComponent.call(this);
        this.setRootNode(new Ext.tree.TreeNode({ text: _('Files') }));
    },

    clear: function() {
        var root = this.getRootNode();
        if (!root.hasChildNodes()) return;
        root.cascade(function(node) {
            var parentNode = node.parentNode;
            if (!parentNode) return;
            if (!parentNode.ownerTree) return;
            parentNode.removeChild(node);
        });
    },

    createFileTree: function(files) {
        function walk(files, parentNode) {
            for (var file in files.contents) {
                var item = files.contents[file];
                if (item.type == 'dir') {
                    walk(
                        item,
                        parentNode.appendChild(
                            new Ext.tree.TreeNode({
                                text: file,
                                filename: file,
                                size: item.size,
                                progress: item.progress,
                                priority: item.priority,
                            })
                        )
                    );
                } else {
                    parentNode.appendChild(
                        new Ext.tree.TreeNode({
                            text: file,
                            filename: file,
                            fileIndex: item.index,
                            size: item.size,
                            progress: item.progress,
                            priority: item.priority,
                            leaf: true,
                            iconCls: 'x-deluge-file',
                            uiProvider: Ext.ux.tree.TreeGridNodeUI,
                        })
                    );
                }
            }
        }
        var root = this.getRootNode();
        walk(files, root);
        root.firstChild.expand();
    },

    update: function(torrentId) {
        if (this.torrentId != torrentId) {
            this.clear();
            this.torrentId = torrentId;
        }

        deluge.client.web.get_torrent_files(torrentId, {
            success: this.onRequestComplete,
            scope: this,
            torrentId: torrentId,
        });
    },

    updateFileTree: function(files) {
        function walk(files, parentNode) {
            for (var file in files.contents) {
                var item = files.contents[file];
                var node = parentNode.findChild('filename', file);
                node.attributes.size = item.size;
                node.attributes.progress = item.progress;
                node.attributes.priority = item.priority;
                node.ui.updateColumns();
                if (item.type == 'dir') {
                    walk(item, node);
                }
            }
        }
        walk(files, this.getRootNode());
    },

    onRender: function(ct, position) {
        Deluge.details.FilesTab.superclass.onRender.call(this, ct, position);
        deluge.menus.filePriorities.on('itemclick', this.onItemClick, this);
        this.on('contextmenu', this.onContextMenu, this);
        this.sorter = new Ext.tree.TreeSorter(this, {
            folderSort: true,
        });
    },

    onContextMenu: function(node, e) {
        e.stopEvent();
        var selModel = this.getSelectionModel();
        if (selModel.getSelectedNodes().length < 2) {
            selModel.clearSelections();
            node.select();
        }
        deluge.menus.filePriorities.showAt(e.getPoint());
    },

    onItemClick: function(baseItem, e) {
        switch (baseItem.id) {
            case 'expandAll':
                this.expandAll();
                break;
            default:
                var indexes = {};
                var walk = function(node) {
                    if (Ext.isEmpty(node.attributes.fileIndex)) return;
                    indexes[node.attributes.fileIndex] =
                        node.attributes.priority;
                };
                this.getRootNode().cascade(walk);

                var nodes = this.getSelectionModel().getSelectedNodes();
                Ext.each(nodes, function(node) {
                    if (!node.isLeaf()) {
                        var setPriorities = function(node) {
                            if (Ext.isEmpty(node.attributes.fileIndex)) return;
                            indexes[node.attributes.fileIndex] =
                                baseItem.filePriority;
                        };
                        node.cascade(setPriorities);
                    } else if (!Ext.isEmpty(node.attributes.fileIndex)) {
                        indexes[node.attributes.fileIndex] =
                            baseItem.filePriority;
                        return;
                    }
                });

                var priorities = new Array(Ext.keys(indexes).length);
                for (var index in indexes) {
                    priorities[index] = indexes[index];
                }

                deluge.client.core.set_torrent_options(
                    [this.torrentId],
                    { file_priorities: priorities },
                    {
                        success: function() {
                            Ext.each(nodes, function(node) {
                                node.setColumnValue(3, baseItem.filePriority);
                            });
                        },
                        scope: this,
                    }
                );
                break;
        }
    },

    onRequestComplete: function(files, options) {
        if (!this.getRootNode().hasChildNodes()) {
            this.createFileTree(files);
        } else {
            this.updateFileTree(files);
        }
    },
});