📄 downloadmanagerdefaultpaths.java
字号:
" - does not match source criteria.", lr);
return null;
}
File target_path = mi.target.getTarget(dm, lr, mi);
if (target_path == null) {
logInfo("Unable to determine an appropriate target for " +
describe(dm, mi) + ".", lr);
return null;
}
logInfo("Determined path for " + describe(dm, mi) + ".", lr);
return mi.transfer.getTransferDetails(dm, lr, mi, target_path);
}
private static TransferDetails determinePaths(DownloadManager dm, MovementInformation[] mis) {
TransferDetails result = null;
for (int i=0; i<mis.length; i++) {
result = determinePaths(dm, mis[i]);
if (result != null) {return result;}
}
return null;
}
public static boolean isInDefaultDownloadDir(DownloadManager dm) {
// We don't create this object properly, but just enough to get it
// to be usable.
SourceSpecification source = new SourceSpecification();
source.setBoolean("default subdir", SUBDIR_PARAM);
return source.checkDefaultDir(dm.getSaveLocation().getParentFile(), getDefaultDirs(null));
}
public static class TransferDetails {
public File transfer_destination;
public boolean move_torrent;
public String toString() {
return "TransferDetails [dest: " + this.transfer_destination.getPath() +
", move-torrent: " + this.move_torrent + "]";
}
}
private static interface ContextDescriptor {
public String getContext();
}
private static class MovementInformation implements ContextDescriptor {
final SourceSpecification source;
final TargetSpecification target;
final TransferSpecification transfer;
final String title;
MovementInformation(SourceSpecification source, TargetSpecification target,
TransferSpecification transfer, String title) {
this.source = source;
this.target = target;
this.transfer = transfer;
this.title = title;
}
public String getContext() {return title;}
}
private abstract static class ParameterHelper implements ContextDescriptor {
private Map settings = new HashMap();
private String context = null;
protected boolean getBoolean(String key) {
Object result = this.settings.get(key);
if (result == null) {throw new RuntimeException("bad key: " + key);}
if (result instanceof Boolean) {return ((Boolean)result).booleanValue();}
return COConfigurationManager.getBooleanParameter((String)result);
}
protected void setBoolean(String key, boolean value) {
settings.put(key, Boolean.valueOf(value));
}
protected void setBoolean(String key, String param) {
settings.put(key, param);
}
protected void setString(String key, String param) {
settings.put(key, param);
}
protected String getString(String key) {
String result = (String)this.settings.get(key);
if (result == null) {throw new RuntimeException("bad key: " + key);}
// This try-catch should be removed, it's only here for debugging purposes.
return COConfigurationManager.getStringParameter(result);
}
public Map getSettings() {return this.settings;}
public void updateSettings(Map settings) {this.settings.putAll(settings);}
public String getContext() {return this.context;}
public void setContext(String context) {this.context = context;}
}
private static class SourceSpecification extends ParameterHelper {
public boolean matchesDownload(DownloadManager dm, LogRelation lr, ContextDescriptor context) {
if (this.getBoolean("persistent only") && !dm.isPersistent()) {
logWarn(describe(dm, context) + " is not persistent.", lr);
return false;
}
if (this.getBoolean("check exclusion flag") && dm.getDownloadState().getFlag(DownloadManagerState.FLAG_DISABLE_AUTO_FILE_MOVE)) {
logWarn(describe(dm, context) + " has exclusion flag set.", lr);
return false;
}
if (this.getBoolean("check completion flag") && dm.getDownloadState().getFlag(DownloadManagerState.FLAG_MOVE_ON_COMPLETION_DONE)) {
logInfo(describe(dm, context) + " has completion flag set.", lr);
return false;
}
if (this.getBoolean("default dir")) {
logInfo("Checking if " + describe(dm, context) + " is inside default dirs.", lr);
File[] default_dirs = getDefaultDirs(lr);
File current_location = dm.getSaveLocation().getParentFile();
if (!this.checkDefaultDir(current_location, default_dirs)) {
logWarn(describe(dm, context) +
" doesn't exist in any of the following default directories" +
" (current dir: " + current_location + ", subdirectories checked: " +
this.getBoolean("default subdir") + ") - " + Arrays.asList(default_dirs), lr);
return false;
}
logInfo(describe(dm, context) + " does exist inside default dirs.", lr);
}
String current_state = getStateDescriptor(dm);
boolean can_move = this.getBoolean(current_state);
String log_message = describe(dm, context) + " is " +
((can_move) ? "" : "not ") + "in an appropriate state (is " +
"currently \"" + current_state + "\").";
if (!can_move) {
logWarn(log_message, lr);
return false;
}
logInfo(log_message, lr);
return true;
}
public boolean checkDefaultDir(File location, File[] default_dirs) {
location = FileUtil.canonise(location);
boolean subdir = this.getBoolean("default subdir");
for (int i=0; i<default_dirs.length; i++) {
if (subdir) {
if (FileUtil.isAncestorOf(default_dirs[i], location)) {return true;}
}
else {
if (default_dirs[i].equals(location)) {return true;}
}
}
return false;
}
}
private static class TargetSpecification extends ParameterHelper {
public File getTarget(DownloadManager dm, LogRelation lr, ContextDescriptor cd) {
//logInfo("Calculating target location for " + describe(dm, cd), lr);
if (!this.getBoolean("enabled")) {
logInfo("Target for " + describe(dm, cd) + " is not enabled.", lr);
return null;
}
String location = this.getString("target").trim();
if (location.length() == 0) {
logInfo("No explicit target for " + describe(dm, cd) + ".", lr);
return null;
}
File target = new File(FileUtil.getCanonicalFileName(location));
String relative_path = null;
if( dm != null && dm.getDownloadState() != null ) {
relative_path = dm.getDownloadState().getRelativeSavePath();
}
if (relative_path != null && relative_path.length() > 0) {
logInfo("Consider relative save path: " + relative_path, lr);
// Doesn't matter if File.separator is required or not, it seems to
// remove duplicate file separators.
target = new File(target.getPath() + File.separator + relative_path);
}
return target;
}
}
private static class TransferSpecification extends ParameterHelper {
public TransferDetails getTransferDetails(DownloadManager dm, LogRelation lr,
ContextDescriptor cd, File target_path) {
if (target_path == null) {throw new NullPointerException();}
TransferDetails result = new TransferDetails();
result.transfer_destination = target_path;
result.move_torrent = this.getBoolean("torrent");
return result;
}
}
public static TransferDetails onCompletion(DownloadManager dm, boolean set_on_completion_flag) {
TransferDetails td = determinePaths(dm, COMPLETION_DETAILS);
// Not sure what we should do if we don't have any transfer details w.r.t the
// completion flag. I think we probably should - we only want to consider the
// settings once - when completion has actually occurred.
if (set_on_completion_flag) {
LogRelation lr = (dm instanceof LogRelation) ? (LogRelation)dm : null;
logInfo("Setting completion flag on " + describe(dm, null) + ", may have been set before.", lr);
dm.getDownloadState().setFlag(DownloadManagerState.FLAG_MOVE_ON_COMPLETION_DONE, true);
}
return td;
}
public static TransferDetails onRemoval(DownloadManager dm) {
return determinePaths(dm, REMOVAL_DETAILS);
}
public static File[] getDefaultSavePaths(DownloadManager dm, boolean for_moving) {
MovementInformation[] mi = (for_moving) ? UPDATE_FOR_MOVE_DETAILS : UPDATE_FOR_LOGIC_DETAILS;
TransferDetails details = determinePaths(dm, mi);
// Always return an array of size two.
File[] result = new File[2];
// Set default values first.
if (!for_moving) {
result[0] = dm.getSaveLocation();
result[1] = new File(dm.getTorrentFileName()).getParentFile();
}
if (details != null) {
result[0] = details.transfer_destination;
if (details.move_torrent) {
result[1] = result[0];
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -