⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadmanagerdefaultpaths.java

📁 java 文件下载器。可自定义
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   DownloadManagerDefaultPaths.java

package org.gudy.azureus2.core3.download.impl;

import java.io.File;
import java.util.*;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.download.DownloadManager;
import org.gudy.azureus2.core3.download.DownloadManagerState;
import org.gudy.azureus2.core3.util.FileUtil;
import org.gudy.azureus2.plugins.download.Download;
import org.gudy.azureus2.plugins.download.savelocation.DefaultSaveLocationManager;
import org.gudy.azureus2.plugins.download.savelocation.SaveLocationChange;
import org.gudy.azureus2.pluginsimpl.local.download.DownloadImpl;

// Referenced classes of package org.gudy.azureus2.core3.download.impl:
//			DownloadManagerMoveHandlerUtils

public class DownloadManagerDefaultPaths extends DownloadManagerMoveHandlerUtils
{
	private static interface ContextDescriptor
	{

		public abstract String getContext();
	}

	private static class MovementInformation
		implements ContextDescriptor
	{

		final SourceSpecification source;
		final TargetSpecification target;
		final TransferSpecification transfer;
		final String title;

		public String getContext()
		{
			return title;
		}

		MovementInformation(SourceSpecification source, TargetSpecification target, TransferSpecification transfer, String title)
		{
			this.source = source;
			this.target = target;
			this.transfer = transfer;
			this.title = title;
		}
	}

	private static abstract class ParameterHelper
		implements ContextDescriptor
	{

		private Map settings;
		private String context;

		protected boolean getBoolean(String key)
		{
			Object result = settings.get(key);
			if (result == null)
				throw new RuntimeException((new StringBuilder()).append("bad key: ").append(key).toString());
			if (result instanceof Boolean)
				return ((Boolean)result).booleanValue();
			else
				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)settings.get(key);
			if (result == null)
				throw new RuntimeException((new StringBuilder()).append("bad key: ").append(key).toString());
			else
				return COConfigurationManager.getStringParameter(result);
		}

		public Map getSettings()
		{
			return settings;
		}

		public void updateSettings(Map settings)
		{
			this.settings.putAll(settings);
		}

		public String getContext()
		{
			return context;
		}

		public void setContext(String context)
		{
			this.context = context;
		}

		private ParameterHelper()
		{
			settings = new HashMap();
			context = null;
		}

	}

	private static class SourceSpecification extends ParameterHelper
	{

		public boolean matchesDownload(DownloadManager dm, ContextDescriptor context)
		{
			if (getBoolean("default dir"))
			{
				DownloadManagerMoveHandlerUtils.logInfo((new StringBuilder()).append("Checking if ").append(DownloadManagerDefaultPaths.describe(dm, context)).append(" is inside default dirs.").toString(), dm);
				File default_dirs[] = DownloadManagerDefaultPaths.getDefaultDirs();
				File current_location = dm.getSaveLocation().getParentFile();
				if (current_location == null)
				{
					DownloadManagerMoveHandlerUtils.logWarn((new StringBuilder()).append(DownloadManagerDefaultPaths.describe(dm, context)).append(" appears to have a malformed save directory, skipping.").toString(), dm);
					return false;
				}
				if (!checkDefaultDir(current_location, default_dirs))
				{
					DownloadManagerMoveHandlerUtils.logWarn((new StringBuilder()).append(DownloadManagerDefaultPaths.describe(dm, context)).append(" doesn't exist in any of the following default directories").append(" (current dir: ").append(current_location).append(", subdirectories checked: ").append(getBoolean("default subdir")).append(") - ").append(Arrays.asList(default_dirs)).toString(), dm);
					return false;
				}
				DownloadManagerMoveHandlerUtils.logInfo((new StringBuilder()).append(DownloadManagerDefaultPaths.describe(dm, context)).append(" does exist inside default dirs.").toString(), dm);
			}
			if (!dm.isDownloadComplete(false))
			{
				boolean can_move = getBoolean("incomplete dl");
				String log_message = (new StringBuilder()).append(DownloadManagerDefaultPaths.describe(dm, context)).append(" is incomplete which is ").append(can_move ? "" : "not ").append("an appropriate state.").toString();
				if (!can_move)
				{
					DownloadManagerMoveHandlerUtils.logInfo(log_message, dm);
					return false;
				}
			}
			return true;
		}

		public boolean checkDefaultDir(File location, File default_dirs[])
		{
			location = FileUtil.canonise(location);
			boolean subdir = getBoolean("default subdir");
			for (int i = 0; i < default_dirs.length; i++)
			{
				if (subdir)
				{
					if (FileUtil.isAncestorOf(default_dirs[i], location))
						return true;
					continue;
				}
				if (default_dirs[i].equals(location))
					return true;
			}

			return false;
		}

		private SourceSpecification()
		{
		}

	}

	private static class TargetSpecification extends ParameterHelper
	{

		public File getTarget(DownloadManager dm, ContextDescriptor cd)
		{
			if (!getBoolean("enabled"))
			{
				DownloadManagerMoveHandlerUtils.logInfo((new StringBuilder()).append("Target for ").append(DownloadManagerDefaultPaths.describe(dm, cd)).append(" is not enabled.").toString(), dm);
				return null;
			}
			String location = getString("target").trim();
			if (location.length() == 0)
			{
				DownloadManagerMoveHandlerUtils.logInfo((new StringBuilder()).append("No explicit target for ").append(DownloadManagerDefaultPaths.describe(dm, cd)).append(".").toString(), dm);
				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)
			{
				DownloadManagerMoveHandlerUtils.logInfo((new StringBuilder()).append("Consider relative save path: ").append(relative_path).toString(), dm);
				target = new File((new StringBuilder()).append(target.getPath()).append(File.separator).append(relative_path).toString());
			}
			return target;
		}

		private TargetSpecification()
		{
		}

	}

	private static class TransferSpecification extends ParameterHelper
	{

		public SaveLocationChange getTransferDetails(DownloadManager dm, ContextDescriptor cd, File target_path)
		{
			if (target_path == null)
				throw new NullPointerException();
			SaveLocationChange result = new SaveLocationChange();
			result.download_location = target_path;
			if (getBoolean("torrent"))
				result.torrent_location = target_path;
			return result;
		}

		private TransferSpecification()
		{
		}

	}


	public static final DefaultSaveLocationManager DEFAULT_HANDLER = new DefaultSaveLocationManager() {

		public SaveLocationChange onInitialization(Download d, boolean for_move, boolean on_event)
		{
			if (on_event)
			{
				return null;
			} else
			{
				DownloadManager dm = ((DownloadImpl)d).getDownload();
				return DownloadManagerDefaultPaths.determinePaths(dm, DownloadManagerDefaultPaths.UPDATE_FOR_MOVE_DETAILS[1], for_move);
			}
		}

		public SaveLocationChange onCompletion(Download d, boolean for_move, boolean on_event)
		{
			DownloadManager dm = ((DownloadImpl)d).getDownload();
			return DownloadManagerDefaultPaths.determinePaths(dm, DownloadManagerDefaultPaths.COMPLETION_DETAILS, for_move);
		}

		public SaveLocationChange onRemoval(Download d, boolean for_move, boolean on_event)
		{
			DownloadManager dm = ((DownloadImpl)d).getDownload();
			return DownloadManagerDefaultPaths.determinePaths(dm, DownloadManagerDefaultPaths.REMOVAL_DETAILS, for_move);
		}

		public boolean isInDefaultSaveDir(Download d)
		{
			DownloadManager dm = ((DownloadImpl)d).getDownload();
			return DownloadManagerDefaultPaths.isInDefaultDownloadDir(dm);
		}

	};
	private static final MovementInformation COMPLETION_DETAILS;
	private static final MovementInformation REMOVAL_DETAILS;
	private static final MovementInformation UPDATE_FOR_MOVE_DETAILS[];
	private static final TargetSpecification DEFAULT_DIRS[];
	private static final String SUBDIR_PARAM = "File.move.subdir_is_default";

	public DownloadManagerDefaultPaths()
	{
	}

	private static String normaliseRelativePathPart(String name)
	{
		name = name.trim();
		if (name.length() == 0)
			return "";
		if (name.equals(".") || name.equals(".."))
			return null;
		else
			return FileUtil.convertOSSpecificChars(name, false).trim();
	}

	public static File normaliseRelativePath(File path)
	{
		if (path.isAbsolute())
			return null;
		File parent = path.getParentFile();
		String child_name = normaliseRelativePathPart(path.getName());
		if (child_name == null)
			return null;
		if (parent == null)
			return new File(child_name);
		ArrayList parts = new ArrayList();
		parts.add(child_name);
		String filepart = null;
		for (; parent != null; parent = parent.getParentFile())
		{
			filepart = normaliseRelativePathPart(parent.getName());
			if (filepart == null)
				return null;
			if (filepart.length() != 0)
				parts.add(0, filepart);
		}

		StringBuffer sb = new StringBuffer((String)parts.get(0));
		for (int i = 1; i < parts.size(); i++)
		{
			sb.append(File.separatorChar);
			sb.append(parts.get(i));
		}

		return new File(sb.toString());
	}

	private static File[] getDefaultDirs()
	{
		List results = new ArrayList();
		File location = null;
		TargetSpecification ts = null;
		for (int i = 0; i < DEFAULT_DIRS.length; i++)
		{
			ts = DEFAULT_DIRS[i];
			location = ts.getTarget(null, ts);
			if (location != null)
				results.add(location);
		}

		return (File[])(File[])results.toArray(new File[results.size()]);
	}

	private static SaveLocationChange determinePaths(DownloadManager dm, MovementInformation mi, boolean check_source)
	{
		boolean proceed = !check_source || mi.source.matchesDownload(dm, mi);
		if (!proceed)
		{
			logInfo((new StringBuilder()).append("Cannot consider ").append(describe(dm, mi)).append(" - does not match source criteria.").toString(), dm);
			return null;
		}
		File target_path = mi.target.getTarget(dm, mi);
		if (target_path == null)
		{
			logInfo((new StringBuilder()).append("Unable to determine an appropriate target for ").append(describe(dm, mi)).append(".").toString(), dm);
			return null;
		} else
		{
			logInfo((new StringBuilder()).append("Determined path for ").append(describe(dm, mi)).append(".").toString(), dm);
			return mi.transfer.getTransferDetails(dm, mi, target_path);
		}
	}

	static boolean isInDefaultDownloadDir(DownloadManager dm)
	{
		SourceSpecification source = new SourceSpecification();
		source.setBoolean("default subdir", "File.move.subdir_is_default");
		return source.checkDefaultDir(dm.getSaveLocation().getParentFile(), getDefaultDirs());
	}

	public static File getCompletionDirectory(DownloadManager dm)
	{
		return COMPLETION_DETAILS.target.getTarget(dm, null);
	}

	static String describe(DownloadManager dm, ContextDescriptor cs)
	{
		if (cs == null)
			return describe(dm);
		if (dm == null)
			return (new StringBuilder()).append("\"").append(cs.getContext()).append("\"").toString();
		else
			return (new StringBuilder()).append("\"").append(dm.getDisplayName()).append("\" with regard to \"").append(cs.getContext()).append("\"").toString();
	}

	static 
	{
		DEFAULT_DIRS = new TargetSpecification[3];
		TargetSpecification dest = new TargetSpecification();
		dest.setBoolean("enabled", true);
		dest.setString("target", "Default save path");
		dest.setContext("default save dir");
		DEFAULT_DIRS[0] = dest;
		SourceSpecification source = new SourceSpecification();
		source.setBoolean("default dir", "Move Only When In Default Save Dir");
		source.setBoolean("default subdir", "File.move.subdir_is_default");
		source.setBoolean("incomplete dl", false);
		dest = new TargetSpecification();
		dest.setBoolean("enabled", "Move Completed When Done");
		dest.setString("target", "Completed Files Directory");
		dest.setContext("completed files dir");
		TransferSpecification trans = new TransferSpecification();
		trans.setBoolean("torrent", "Move Torrent When Done");
		MovementInformation mi_1 = new MovementInformation(source, dest, trans, "Move on completion");
		COMPLETION_DETAILS = mi_1;
		DEFAULT_DIRS[1] = dest;
		source = new SourceSpecification();
		source.setBoolean("default dir", "File.move.download.removed.only_in_default");
		source.setBoolean("default subdir", "File.move.subdir_is_default");
		source.setBoolean("incomplete dl", false);
		dest = new TargetSpecification();
		dest.setBoolean("enabled", "File.move.download.removed.enabled");
		dest.setString("target", "File.move.download.removed.path");
		dest.setContext("removed files dir");
		trans = new TransferSpecification();
		trans.setBoolean("torrent", "File.move.download.removed.move_torrent");
		mi_1 = new MovementInformation(source, dest, trans, "Move on removal");
		REMOVAL_DETAILS = mi_1;
		DEFAULT_DIRS[2] = dest;
		source = new SourceSpecification();
		source.updateSettings(COMPLETION_DETAILS.source.getSettings());
		source.setBoolean("default dir", true);
		mi_1 = new MovementInformation(source, COMPLETION_DETAILS.target, COMPLETION_DETAILS.transfer, "Update completed download");
		source = new SourceSpecification();
		source.setBoolean("default dir", true);
		source.setBoolean("default subdir", "File.move.subdir_is_default");
		source.setBoolean("incomplete dl", true);
		dest = new TargetSpecification();
		dest.setBoolean("enabled", true);
		dest.setString("target", "Default save path");
		trans = new TransferSpecification();
		trans.setBoolean("torrent", false);
		MovementInformation mi_2 = new MovementInformation(source, dest, trans, "Update incomplete download");
		UPDATE_FOR_MOVE_DETAILS = (new MovementInformation[] {
			mi_1, mi_2
		});
	}





}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -