      1 /*
      2 Script: Deluge.Details.Files.js
      3     The files tab displayed in the details panel.
      4
      5 Copyright:
      6 	(C) Damien Churchill 2009 <damoxc@gmail.com>
      7 	This program is free software; you can redistribute it and/or modify
      8 	it under the terms of the GNU General Public License as published by
      9 	the Free Software Foundation; either version 3, or (at your option)
     10 	any later version.
     11
     12 	This program is distributed in the hope that it will be useful,
     13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 	GNU General Public License for more details.
     16
     17 	You should have received a copy of the GNU General Public License
     18 	along with this program.  If not, write to:
     19 		The Free Software Foundation, Inc.,
     20 		51 Franklin Street, Fifth Floor
     21 		Boston, MA  02110-1301, USA.
     22 #
     23 #    In addition, as a special exception, the copyright holders give
     24 #    permission to link the code of portions of this program with the OpenSSL
     25 #    library.
     26 #    You must obey the GNU General Public License in all respects for all of
     27 #    the code used other than OpenSSL. If you modify file(s) with this
     28 #    exception, you may extend this exception to your version of the file(s),
     29 #    but you are not obligated to do so. If you do not wish to do so, delete
     30 #    this exception statement from your version. If you delete this exception
     31 #    statement from all source files in the program, then also delete it here.
     32 #
     33 */
     34 (function() {
     35 	/* Renderers for the column tree */
     36 	function fileProgressRenderer(value) {
     37 		var progress = value * 100;
     38 		return Deluge.progressBar(progress, this.width - 50, progress.toFixed(2) + '%');
     39 	}
     40 	function priorityRenderer(value) {
     41 		return String.format('<div class="{0}">{1}</div>', FILE_PRIORITY_CSS[value], _(FILE_PRIORITY[value]));
     42 	}
     43
     44 	Ext.deluge.details.FilesTab = Ext.extend(Ext.tree.ColumnTree, {
     45
     46 		constructor: function(config) {
     47 			config = Ext.apply({
     48 				title: _('Files'),
     49 				rootVisible: false,
     50 				autoScroll: true,
     51 				selModel: new Ext.tree.MultiSelectionModel(),
     52
     53 				columns: [{
     54 					header: _('Filename'),
     55 					width: 330,
     56 					dataIndex: 'filename'
     57 				}, {
     58 					header: _('Size'),
     59 					width: 150,
     60 					dataIndex: 'size',
     61 					renderer: fsize
     62 				}, {
     63 					header: _('Progress'),
     64 					width: 150,
     65 					dataIndex: 'progress',
     66 					renderer: fileProgressRenderer
     67 				}, {
     68 					header: _('Priority'),
     69 					width: 150,
     70 					dataIndex: 'priority',
     71 					renderer: priorityRenderer
     72 				}],
     73
     74 				root: new Ext.tree.TreeNode({
     75 					text: 'Files'
     76 				})
     77 			}, config);
     78 			Ext.deluge.details.FilesTab.superclass.constructor.call(this, config);
     79 		},
     80
     81 		onRender: function(ct, position) {
     82 			Ext.deluge.details.FilesTab.superclass.onRender.call(this, ct, position);
     83 			Deluge.Menus.FilePriorities.on('itemclick', this.onItemClick, this);
     84 			this.on('contextmenu', this.onContextMenu, this);
     85 			this.sorter = new Ext.tree.TreeSorter(this, {
     86 				folderSort: true
     87 			});
     88 		},
     89
     90 		clear: function() {
     91 			var root = this.getRootNode();
     92 			if (!root.hasChildNodes()) return;
     93 			root.cascade(function(node) {
     94 				var parent = node.parentNode;
     95 				if (!parent) return;
     96 				if (!parent.ownerTree) return;
     97 				parent.removeChild(node);
     98 			});
     99 		},
    100
    101 		update: function(torrentId) {
    102 			if (this.torrentId != torrentId) {
    103 				this.clear();
    104 				this.torrentId = torrentId;
    105 			}
    106
    107 			Deluge.Client.web.get_torrent_files(torrentId, {
    108 				success: this.onRequestComplete,
    109 				scope: this,
    110 				torrentId: torrentId
    111 			});
    112 		},
    113
    114 		onContextMenu: function(node, e) {
    115 			e.stopEvent();
    116 			var selModel = this.getSelectionModel();
    117 			if (selModel.getSelectedNodes().length < 2) {
    118 				selModel.clearSelections();
    119 				node.select();
    120 			}
    121 			Deluge.Menus.FilePriorities.showAt(e.getPoint());
    122 		},
    123
    124 		onItemClick: function(baseItem, e) {
    125 			switch (baseItem.id) {
    126 				case 'expandAll':
    127 					this.expandAll();
    128 					break;
    129 				default:
    130 					var indexes = {};
    131 					function walk(node) {
    132 						if (Ext.isEmpty(node.attributes.fileIndex)) return;
    133 						indexes[node.attributes.fileIndex] = node.attributes.priority;
    134 					}
    135 					this.getRootNode().cascade(walk);
    136
    137 					var nodes = this.getSelectionModel().getSelectedNodes();
    138 					Ext.each(nodes, function(node) {
    139 						if (Ext.isEmpty(node.attributes.fileIndex)) return;
    140 						indexes[node.attributes.fileIndex] = baseItem.filePriority;
    141 					});
    142
    143 					var priorities = new Array(Ext.keys(indexes).length);
    144 					for (var index in indexes) {
    145 						priorities[index] = indexes[index];
    146 					}
    147
    148 					Deluge.Client.core.set_torrent_file_priorities(this.torrentId, priorities, {
    149 						success: function() {
    150 							Ext.each(nodes, function(node) {
    151 								node.setColumnValue(3, baseItem.filePriority);
    152 							});
    153 						},
    154 						scope: this
    155 					});
    156 					break;
    157 			}
    158 		},
    159
    160 		onRequestComplete: function(files, options) {
    161 			function walk(files, parent) {
    162 				for (var file in files) {
    163 					var item = files[file];
    164 					var child = parent.findChild('id', file);
    165 					if (Ext.type(item) == 'object') {
    166 						if (!child) {
    167 							child = new Ext.tree.TreeNode({
    168 								id: file,
    169 								text: file
    170 							});
    171 							parent.appendChild(child);
    172 						}
    173 						walk(item, child);
    174 					} else {
    175 						if (!child) {
    176 							child = new Ext.tree.ColumnTreeNode({
    177 								id: file,
    178 								filename: file,
    179 								text: file, // this needs to be here for sorting
    180 								fileIndex: item[0],
    181 								size: item[1],
    182 								progress: item[2],
    183 								priority: item[3],
    184 								leaf: true,
    185 								iconCls: 'x-deluge-file',
    186 								uiProvider: Ext.tree.ColumnNodeUI
    187 							});
    188 							parent.appendChild(child);
    189 						}
    190 						child.setColumnValue(1, item[1]);
    191 						child.setColumnValue(2, item[2]);
    192 						child.setColumnValue(3, item[3]);
    193 					}
    194 				}
    195 			}
    196 			var root = this.getRootNode();
    197 			walk(files, root);
    198 			root.firstChild.expand();
    199 		}
    200 	});
    201 	Deluge.Details.add(new Ext.deluge.details.FilesTab());
    202 })();