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

View File

@ -364,7 +364,7 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
reset: function() {
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'];
Deluge.Client.core.set_torrent_prioritize_first_last(this.torrentId, value, {
success: function() {
this.optionsManager.setOption(this.torrentId, 'prioritize_first_last', value);
this.optionsManager.set(this.torrentId, 'prioritize_first_last', value);
},
scope: this
});
}
Deluge.Client.core.set_torrent_options([this.torrentId], changed, {
success: function() {
this.optionsManager.setOptions(this.torrentId, changed);
this.optionsManager.resetOptions(this.torrentId);
this.optionsManager.set(this.torrentId, changed);
this.optionsManager.reset(this.torrentId);
},
scope: this
});
@ -411,7 +411,7 @@ Ext.deluge.details.OptionsTab = Ext.extend(Ext.form.FormPanel, {
this.fields['private'].setDisabled(!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());

View File

@ -95,12 +95,30 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
this.currentId = id;
for (var option in this.defaults) {
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
* @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} option
* @param {String|Array} [option] A single option or an array of options to return.
* @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];
},
/**
* 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.
* @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.
* @param {String} id
*/
resetOptions: function(id) {
reset: function(id) {
if (!this.changed[id]) return;
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} option
* @param {Object} value The value for the option
*/
setOption: function(id, option, value) {
this.options[id][option] = value;
},
/**
* Set the specified options for the passed in id.
* @param {String} id
* @param {Object} options The option values to change.
*/
setOptions: function(id, options) {
if (!this.changed[id]) this.changed[id] = {};
for (var key in options) {
this.setOption(id, key, options[key]);
set: function(id, option, value) {
if (typeof value === undefined) {
for (var key in option) {
this.set(id, key, option[key]);
}
} else {
if (!this.options[id]) this.options[id] = {};
this.options[id][option] = value;
}
},
/**
* Update the value for the specified option and id.
* @param {String} id
* @param {String} option
* @param {Object} value;
* @param {String|Object} option or options to update
* @param {Object} [value];
*/
updateOption: function(id, option, value) {
if (!this.changed[id]) this.changed[id] = {};
var oldValue = this.getValue(id, option);
if (oldValue == value) return;
var defaultValue = this.getOption(id, option);
if (defaultValue == value) {
if (!Ext.isEmpty(this.changed[id][option])) delete this.changed[id][option];
this.fireEvent('changed', id, option, value, oldValue);
return;
}
if (Ext.type(defaultValue) != Ext.type(value)) {
switch (Ext.type(defaultValue)) {
case 'string':
value = String(value);
break;
case 'number':
value = Number(value);
break;
case 'boolean':
value = Boolean(value);
break;
update: function(id, option, value) {
if (typeof value === undefined) {
for (var key in option) {
this.update(id, key, option[key]);
}
} else {
if (!this.changed[id]) this.changed[id] = {};
var oldValue = this.get(id, option);
if (oldValue == value) return;
var defaultValue = this.getDefault(id, option);
if (defaultValue == value) {
if (this.hasChanged(id, option)) delete this.changed[id][option];
this.fireEvent('changed', id, option, value, oldValue);
return;
}
if (Ext.type(defaultValue) != Ext.type(value)) {
switch (Ext.type(defaultValue)) {
case 'string':
value = String(value);
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);
},
/**
* Update the options for the specified id.
* @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]);
this.changed[id][option] = value;
this.fireEvent('changed', id, option, value, oldValue);
}
},
@ -237,7 +235,7 @@ Deluge.OptionsManager = Ext.extend(Ext.util.Observable, {
*/
onFieldChange: function(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) {

File diff suppressed because one or more lines are too long