1 | /*!
|
---|
2 | * Deluge.add.OptionsPanel.js
|
---|
3 | *
|
---|
4 | * Copyright (c) Damien Churchill 2009-2010 <damoxc@gmail.com>
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 3, or (at your option)
|
---|
9 | * any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program. If not, write to:
|
---|
18 | * The Free Software Foundation, Inc.,
|
---|
19 | * 51 Franklin Street, Fifth Floor
|
---|
20 | * Boston, MA 02110-1301, USA.
|
---|
21 | *
|
---|
22 | * In addition, as a special exception, the copyright holders give
|
---|
23 | * permission to link the code of portions of this program with the OpenSSL
|
---|
24 | * library.
|
---|
25 | * You must obey the GNU General Public License in all respects for all of
|
---|
26 | * the code used other than OpenSSL. If you modify file(s) with this
|
---|
27 | * exception, you may extend this exception to your version of the file(s),
|
---|
28 | * but you are not obligated to do so. If you do not wish to do so, delete
|
---|
29 | * this exception statement from your version. If you delete this exception
|
---|
30 | * statement from all source files in the program, then also delete it here.
|
---|
31 | */
|
---|
32 | Ext.ns('Deluge.add');
|
---|
33 |
|
---|
34 | Deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
|
---|
35 |
|
---|
36 | torrents: {},
|
---|
37 |
|
---|
38 | // layout options
|
---|
39 | region: 'south',
|
---|
40 | margins: '5 5 5 5',
|
---|
41 | activeTab: 0,
|
---|
42 | height: 220,
|
---|
43 |
|
---|
44 | initComponent: function() {
|
---|
45 | Deluge.add.OptionsPanel.superclass.initComponent.call(this);
|
---|
46 | this.files = this.add(new Deluge.add.FilesTab());
|
---|
47 | this.form = this.add(new Deluge.add.OptionsTab());
|
---|
48 |
|
---|
49 | this.files.on('filechecked', this.onFileChecked, this);
|
---|
50 | },
|
---|
51 |
|
---|
52 | addTorrent: function(torrent) {
|
---|
53 | this.torrents[torrent['info_hash']] = torrent;
|
---|
54 | var fileIndexes = {};
|
---|
55 | this.walkFileTree(torrent['files_tree'], function(filename, type, entry, parent) {
|
---|
56 | if (type != 'file') return;
|
---|
57 | fileIndexes[entry.index] = entry.download;
|
---|
58 | }, this);
|
---|
59 |
|
---|
60 | var priorities = [];
|
---|
61 | Ext.each(Ext.keys(fileIndexes), function(index) {
|
---|
62 | priorities[index] = fileIndexes[index];
|
---|
63 | });
|
---|
64 |
|
---|
65 | var oldId = this.form.optionsManager.changeId(torrent['info_hash'], true);
|
---|
66 | this.form.optionsManager.setDefault('file_priorities', priorities);
|
---|
67 | this.form.optionsManager.changeId(oldId, true);
|
---|
68 | },
|
---|
69 |
|
---|
70 | clear: function() {
|
---|
71 | this.files.clearFiles();
|
---|
72 | this.form.optionsManager.resetAll();
|
---|
73 | },
|
---|
74 |
|
---|
75 | getFilename: function(torrentId) {
|
---|
76 | return this.torrents[torrentId]['filename'];
|
---|
77 | },
|
---|
78 |
|
---|
79 | getOptions: function(torrentId) {
|
---|
80 | var oldId = this.form.optionsManager.changeId(torrentId, true);
|
---|
81 | var options = this.form.optionsManager.get();
|
---|
82 | this.form.optionsManager.changeId(oldId, true);
|
---|
83 | Ext.each(options['file_priorities'], function(priority, index) {
|
---|
84 | options['file_priorities'][index] = (priority) ? 1 : 0;
|
---|
85 | });
|
---|
86 | return options;
|
---|
87 | },
|
---|
88 |
|
---|
89 | setTorrent: function(torrentId) {
|
---|
90 | if (!torrentId) return;
|
---|
91 |
|
---|
92 | this.torrentId = torrentId;
|
---|
93 | this.form.optionsManager.changeId(torrentId);
|
---|
94 |
|
---|
95 | this.files.clearFiles();
|
---|
96 | var root = this.files.getRootNode();
|
---|
97 | var priorities = this.form.optionsManager.get('file_priorities');
|
---|
98 |
|
---|
99 | this.walkFileTree(this.torrents[torrentId]['files_tree'], function(filename, type, entry, parentNode) {
|
---|
100 | var node = new Ext.tree.TreeNode({
|
---|
101 | download: (entry.index) ? priorities[entry.index] : true,
|
---|
102 | filename: filename,
|
---|
103 | fileindex: entry.index,
|
---|
104 | leaf: type != 'dir',
|
---|
105 | size: entry.length
|
---|
106 | });
|
---|
107 | parentNode.appendChild(node);
|
---|
108 | if (type == 'dir') return node;
|
---|
109 | }, this, root);
|
---|
110 | root.firstChild.expand();
|
---|
111 | },
|
---|
112 |
|
---|
113 | walkFileTree: function(files, callback, scope, parentNode) {
|
---|
114 | for (var filename in files.contents) {
|
---|
115 | var entry = files.contents[filename];
|
---|
116 | var type = entry.type;
|
---|
117 |
|
---|
118 | if (scope) {
|
---|
119 | var ret = callback.apply(scope, [filename, type, entry, parentNode]);
|
---|
120 | } else {
|
---|
121 | var ret = callback(filename, type, entry, parentNode);
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (type == 'dir') this.walkFileTree(entry, callback, scope, ret);
|
---|
125 | }
|
---|
126 | },
|
---|
127 |
|
---|
128 | onFileChecked: function(node, newValue, oldValue) {
|
---|
129 | if (!Ext.isNumber(node.attributes.fileindex)) return;
|
---|
130 |
|
---|
131 | // if (this.form.optionsManager.get('compact_allocation')) {
|
---|
132 | // Ext.Msg.show({
|
---|
133 | // title: _('Unable to set file priority!'),
|
---|
134 | // msg: _('File prioritization is unavailable when using Compact allocation. Would you like to switch to Full allocation?'),
|
---|
135 | // buttons: Ext.Msg.YESNO,
|
---|
136 | // fn: function(result) {
|
---|
137 | // if (result == 'yes') {
|
---|
138 | // var priorities = this.form.optionsManager.get('file_priorities');
|
---|
139 | // priorities[node.attributes.fileindex] = (result) ? newValue : oldValue;
|
---|
140 | // this.form.optionsManager.update('file_priorities', priorities);
|
---|
141 | // } else {
|
---|
142 | // node.attributes.download = oldValue;
|
---|
143 | // node.ui.updateColumns();
|
---|
144 | // }
|
---|
145 | // },
|
---|
146 | // scope: this,
|
---|
147 | // icon: Ext.MessageBox.QUESTION
|
---|
148 | // });
|
---|
149 | // } else {
|
---|
150 | // var priorities = this.form.optionsManager.get('file_priorities');
|
---|
151 | // priorities[node.attributes.fileindex] = newValue;
|
---|
152 | // this.form.optionsManager.update('file_priorities', priorities);
|
---|
153 | // }
|
---|
154 | var priorities = this.form.optionsManager.get('file_priorities');
|
---|
155 | priorities[node.attributes.fileindex] = newValue;
|
---|
156 | this.form.optionsManager.update('file_priorities', priorities);
|
---|
157 | }
|
---|
158 | });
|
---|