large changes within the optionsmanager

fix the details panel to reflect this
add setting of file priorities in the add window
This commit is contained in:
Damien Churchill 2009-07-12 23:27:41 +00:00
parent 324679de1f
commit 2156d1f6f6
4 changed files with 129 additions and 119 deletions

View File

@ -80,7 +80,7 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
defaults: { defaults: {
'add_paused': false, 'add_paused': false,
'compact_allocation': false, 'compact_allocation': false,
'download_location': '~', 'download_location': '',
'max_connections_per_torrent': -1, 'max_connections_per_torrent': -1,
'max_download_speed_per_torrent': -1, 'max_download_speed_per_torrent': -1,
'max_upload_slots_per_torrent': -1, 'max_upload_slots_per_torrent': -1,
@ -215,8 +215,17 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
addTorrent: function(torrent) { addTorrent: function(torrent) {
this.torrents[torrent['info_hash']] = torrent; this.torrents[torrent['info_hash']] = torrent;
var fileIndexes = {};
this.walkFileTree(torrent['files_tree'], function(filename, type, entry, parent) { this.walkFileTree(torrent['files_tree'], function(filename, type, entry, parent) {
if (type != 'file') return;
fileIndexes[entry[0]] = entry[2];
}, this); }, this);
var priorities = [];
Ext.each(Ext.keys(fileIndexes), function(index) {
priorities[index] = fileIndexes[index];
});
this.optionsManager.set(torrent['info_hash'], 'file_priorities', priorities);
}, },
clear: function() { clear: function() {
@ -233,52 +242,47 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
}, },
getDefaults: function() { getDefaults: function() {
var keys = [ var keys = ['add_paused','compact_allocation','download_location',
'add_paused', 'max_connections_per_torrent','max_download_speed_per_torrent',
'compact_allocation', 'max_upload_slots_per_torrent','max_upload_speed_per_torrent',
'download_location', 'prioritize_first_last_pieces'];
'max_connections_per_torrent',
'max_download_speed_per_torrent',
'max_upload_slots_per_torrent',
'max_upload_speed_per_torrent',
'prioritize_first_last_pieces'
]
Deluge.Client.core.get_config_values(keys, { Deluge.Client.core.get_config_values(keys, {
success: function(config) { success: function(config) {
this.defaults = config; config['file_priorities'] = [];
for (var key in config) { this.optionsManager.defaults = config;
var field = this.form.findField(key);
if (!field) return;
field.setValue(config[key]);
}
var field = this.form.findField('compact_allocation');
if (config['compact_allocation']) {
field.items.get('compact_allocation_true').setValue(true);
field.items.get('compact_allocation_false').setValue(false);
} else {
field.items.get('compact_allocation_false').setValue(true);
field.items.get('compact_allocation_true').setValue(false);
}
}, },
scope: this scope: this
}); });
}, },
getFilename: function(torrentId) {
return this.torrents[torrentId]['filename'];
},
getOptions: function(torrentId) { getOptions: function(torrentId) {
var options = this.optionsManager.get(torrentId);
Ext.each(options['file_priorities'], function(priority, index) {
options['file_priorities'][index] = (priority) ? 1 : 0;
});
return options;
}, },
setTorrent: function(torrentId) { setTorrent: function(torrentId) {
var self = this; this.torrentId = torrentId;
this.optionsManager.changeId(torrentId);
this.clearFiles(); this.clearFiles();
var root = this.files.getRootNode(); var root = this.files.getRootNode();
var priorities = this.optionsManager.get(this.torrentId, 'file_priorities');
this.walkFileTree(this.torrents[torrentId]['files_tree'], function(filename, type, entry, parent) { this.walkFileTree(this.torrents[torrentId]['files_tree'], function(filename, type, entry, parent) {
if (type == 'dir') { if (type == 'dir') {
var folder = new Ext.tree.TreeNode({ var folder = new Ext.tree.TreeNode({
text: filename, text: filename,
checked: true checked: true
}); });
folder.on('checkchange', this.onFolderCheck, self); folder.on('checkchange', this.onFolderCheck, this);
parent.appendChild(folder); parent.appendChild(folder);
return folder; return folder;
} else { } else {
@ -288,11 +292,11 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
text: filename, // this needs to be here for sorting reasons text: filename, // this needs to be here for sorting reasons
size: fsize(entry[1]), size: fsize(entry[1]),
leaf: true, leaf: true,
checked: entry[2], checked: priorities[entry[0]],
iconCls: 'x-deluge-file', iconCls: 'x-deluge-file',
uiProvider: Ext.tree.ColumnNodeUI uiProvider: Ext.tree.ColumnNodeUI
}); });
node.on('checkchange', this.onNodeCheck, self); node.on('checkchange', this.onNodeCheck, this);
parent.appendChild(node); parent.appendChild(node);
} }
}, this, root); }, this, root);
@ -310,20 +314,27 @@ Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
var ret = callback(filename, type, entry, parent); var ret = callback(filename, type, entry, parent);
} }
parent = (ret) ? ret : parent; if (type == 'dir') this.walkFileTree(entry, callback, scope, ret);
if (type == 'dir') this.walkFileTree(entry, callback, scope, parent);
} }
}, },
onFolderCheck: function(node, checked) { onFolderCheck: function(node, checked) {
var priorities = this.optionsManager.get(this.torrentId, 'file_priorities');
node.cascade(function(child) { node.cascade(function(child) {
if (!child.ui.checkbox) return; if (!child.ui.checkbox) {
child.ui.checkbox.checked = checked; child.attributes.checked = checked;
} else {
child.ui.checkbox.checked = checked;
}
priorities[child.attributes.fileindex] = checked;
}, this); }, this);
this.optionsManager.update(this.torrentId, 'file_priorities', priorities);
}, },
onNodeCheck: function(node, checked) { onNodeCheck: function(node, checked) {
var priorities = this.optionsManager.get(this.torrentId, 'file_priorities');
priorities[node.attributes.fileindex] = checked;
this.optionsManager.update(this.torrentId, 'file_priorities', priorities);
} }
}); });
@ -447,16 +458,15 @@ Ext.deluge.add.AddWindow = Ext.extend(Ext.deluge.add.Window, {
}, },
onAdd: function() { onAdd: function() {
var priorities = this.optionsPanel.getFilePriorities(); var torrents = [];
return; this.grid.getStore().each(function(r) {
torrents = []; var id = r.get('info_hash');
for (var id in this.torrents) {
var info = this.torrents[id];
torrents.push({ torrents.push({
path: info['filename'], path: this.optionsPanel.getFilename(id),
options: {} options: this.optionsPanel.getOptions(id)
}); });
} }, this);
Deluge.Client.web.add_torrents(torrents, { Deluge.Client.web.add_torrents(torrents, {
success: function(result) { success: function(result) {
} }
@ -500,6 +510,8 @@ Ext.deluge.add.AddWindow = Ext.extend(Ext.deluge.add.Window, {
this.file.on('beforeadd', this.onTorrentBeforeAdd, this); this.file.on('beforeadd', this.onTorrentBeforeAdd, this);
this.file.on('add', this.onTorrentAdd, this); this.file.on('add', this.onTorrentAdd, this);
} }
this.optionsPanel.getDefaults();
}, },
onTorrentBeforeAdd: function(torrentId, text) { onTorrentBeforeAdd: function(torrentId, text) {

View File

@ -364,7 +364,7 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
reset: function() { reset: function() {
if (this.torrentId) { if (this.torrentId) {
this.optionsManager.resetOptions(this.torrentId); this.optionsManager.reset(this.torrentId);
} }
}, },
@ -382,15 +382,15 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
var value = changed['prioritize_first_last']; var value = changed['prioritize_first_last'];
Deluge.Client.core.set_torrent_prioritize_first_last(this.torrentId, value, { Deluge.Client.core.set_torrent_prioritize_first_last(this.torrentId, value, {
success: function() { success: function() {
this.optionsManager.setOption(this.torrentId, 'prioritize_first_last', value); this.optionsManager.set(this.torrentId, 'prioritize_first_last', value);
}, },
scope: this scope: this
}); });
} }
Deluge.Client.core.set_torrent_options([this.torrentId], changed, { Deluge.Client.core.set_torrent_options([this.torrentId], changed, {
success: function() { success: function() {
this.optionsManager.setOptions(this.torrentId, changed); this.optionsManager.set(this.torrentId, changed);
this.optionsManager.resetOptions(this.torrentId); this.optionsManager.reset(this.torrentId);
}, },
scope: this scope: this
}); });
@ -411,7 +411,7 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
this.fields['private'].setDisabled(!torrent['private']); this.fields['private'].setDisabled(!torrent['private']);
delete torrent['private']; delete torrent['private'];
this.optionsManager.updateOptions(this.torrentId, torrent); this.optionsManager.update(this.torrentId, torrent);
} }
}); });
Deluge.Details.add(new Ext.deluge.details.OptionsTab()); Deluge.Details.add(new Ext.deluge.details.OptionsTab());

View File

@ -95,12 +95,30 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
this.currentId = id; this.currentId = id;
for (var option in this.defaults) { for (var option in this.defaults) {
if (!this.binds[option]) continue; if (!this.binds[option]) continue;
this.binds[option].setValue(this.getValue(id, option)); this.binds[option].setValue(this.get(id, option));
} }
}, },
/** /**
* Returns the changed values for a specified id. * Get the value for an option
* @param {String} id
* @param {String|Array} [option] A single option or an array of options to return.
* @returns {Object} the options value.
*/
get: function(id, option) {
if (!option) {
var values = {};
for (var key in this.defaults) {
values[key] = this.get(id, key);
}
return values;
} else {
return (this.hasChanged(id, option)) ? this.changed[id][option] : this.getDefault(id, option);
}
},
/**
* Returns the changed values.
* @param {String} id * @param {String} id
* @returns {Object} the changed options * @returns {Object} the changed options
*/ */
@ -109,25 +127,15 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
}, },
/** /**
* Get the default value for an option given an id and option name. * Get the default value for an option.
* @param {String} id * @param {String} id
* @param {String} option * @param {String|Array} [option] A single option or an array of options to return.
* @returns {Object} the value of the option * @returns {Object} the value of the option
*/ */
getOption: function(id, option) { getDefault: function(id, option) {
return (this.hasOption(id, option)) ? this.options[id][option] : this.defaults[option]; return (this.hasOption(id, option)) ? this.options[id][option] : this.defaults[option];
}, },
/**
* Get the value for an option given an id and option name.
* @param {String} id
* @param {String} option
* @returns {Object} the options value.
*/
getValue: function(id, option) {
return (this.hasChanged(id, option)) ? this.changed[id][option] : this.getOption(id, option);
},
/** /**
* Check to see if the option has been changed. * Check to see if the option has been changed.
* @param {String} id * @param {String} id
@ -153,78 +161,68 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
* Reset the options back to the default values for the specified id. * Reset the options back to the default values for the specified id.
* @param {String} id * @param {String} id
*/ */
resetOptions: function(id) { reset: function(id) {
if (!this.changed[id]) return; if (!this.changed[id]) return;
delete this.changed[id]; delete this.changed[id];
}, },
/** /**
* Set the specified option for the passed in id. * Sets the value of specified option for the passed in id.
* @param {String} id * @param {String} id
* @param {String} option * @param {String} option
* @param {Object} value The value for the option * @param {Object} value The value for the option
*/ */
setOption: function(id, option, value) { set: function(id, option, value) {
this.options[id][option] = value; if (typeof value === undefined) {
}, for (var key in option) {
this.set(id, key, option[key]);
/** }
* Set the specified options for the passed in id. } else {
* @param {String} id if (!this.options[id]) this.options[id] = {};
* @param {Object} options The option values to change. this.options[id][option] = value;
*/
setOptions: function(id, options) {
if (!this.changed[id]) this.changed[id] = {};
for (var key in options) {
this.setOption(id, key, options[key]);
} }
}, },
/** /**
* Update the value for the specified option and id. * Update the value for the specified option and id.
* @param {String} id * @param {String} id
* @param {String} option * @param {String|Object} option or options to update
* @param {Object} value; * @param {Object} [value];
*/ */
updateOption: function(id, option, value) { update: function(id, option, value) {
if (!this.changed[id]) this.changed[id] = {}; if (typeof value === undefined) {
for (var key in option) {
var oldValue = this.getValue(id, option); this.update(id, key, option[key]);
if (oldValue == value) return; }
} else {
var defaultValue = this.getOption(id, option); if (!this.changed[id]) this.changed[id] = {};
if (defaultValue == value) {
if (!Ext.isEmpty(this.changed[id][option])) delete this.changed[id][option]; var oldValue = this.get(id, option);
this.fireEvent('changed', id, option, value, oldValue); if (oldValue == value) return;
return;
} var defaultValue = this.getDefault(id, option);
if (defaultValue == value) {
if (Ext.type(defaultValue) != Ext.type(value)) { if (this.hasChanged(id, option)) delete this.changed[id][option];
switch (Ext.type(defaultValue)) { this.fireEvent('changed', id, option, value, oldValue);
case 'string': return;
value = String(value); }
break;
case 'number': if (Ext.type(defaultValue) != Ext.type(value)) {
value = Number(value); switch (Ext.type(defaultValue)) {
break; case 'string':
case 'boolean': value = String(value);
value = Boolean(value); break;
break; case 'number':
value = Number(value);
break;
case 'boolean':
value = Boolean(value);
break;
}
} }
}
this.changed[id][option] = value;
this.fireEvent('changed', id, option, value, oldValue);
},
/** this.changed[id][option] = value;
* Update the options for the specified id. this.fireEvent('changed', id, option, value, oldValue);
* @param {String} id
* @param {Object} options The options to change.
*/
updateOptions: function(id, options) {
for (var key in options) {
this.updateOption(id, key, options[key]);
} }
}, },
@ -237,7 +235,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
*/ */
onFieldChange: function(field) { onFieldChange: function(field) {
var option = this.binds[field]; var option = this.binds[field];
this.updateOption(this.currentId, option, field.getValue()); this.update(this.currentId, option, field.getValue());
}, },
onChange: function(id, option, newValue, oldValue) { onChange: function(id, option, newValue, oldValue) {

File diff suppressed because one or more lines are too long