1 /*
2  * Created on 23 May 2008
3  * Created by Allan Crooks
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18 package org.gudy.azureus2.core3.download.impl;
19 
20 import java.io.File;
21 import java.util.ArrayList;
22 
23 import org.gudy.azureus2.core3.config.COConfigurationManager;
24 import org.gudy.azureus2.core3.download.DownloadManager;
25 import org.gudy.azureus2.core3.download.DownloadManagerState;
26 import org.gudy.azureus2.plugins.download.Download;
27 import org.gudy.azureus2.plugins.download.savelocation.SaveLocationChange;
28 import org.gudy.azureus2.plugins.download.savelocation.SaveLocationManager;
29 import org.gudy.azureus2.pluginsimpl.local.PluginCoreUtils;
30 
31 /**
32  * @author Allan Crooks
33  *
34  */
35 public class DownloadManagerMoveHandler extends DownloadManagerMoveHandlerUtils {
36 
37 	public static SaveLocationManager CURRENT_HANDLER = DownloadManagerDefaultPaths.DEFAULT_HANDLER;
38 
isApplicableDownload(DownloadManager dm)39 	private static boolean isApplicableDownload(DownloadManager dm) {
40 		if (!dm.isPersistent()) {
41 			logInfo(describe(dm) + " is not persistent.", dm);
42 			return false;
43 		}
44 
45 		if (dm.getDownloadState().getFlag(DownloadManagerState.FLAG_DISABLE_AUTO_FILE_MOVE)) {
46 			logInfo(describe(dm) + " has exclusion flag set.", dm);
47 			return false;
48 		}
49 
50 		return true;
51 	}
52 
onInitialisation(DownloadManager dm)53 	public static SaveLocationChange onInitialisation(DownloadManager dm) {
54 		if (!isApplicableDownload(dm)) {return null;}
55 		try {return CURRENT_HANDLER.onInitialization(PluginCoreUtils.wrap(dm), true, true);}
56 		catch (Exception e) {
57 			logError("Error trying to determine initial download location.", dm, e);
58 			return null;
59 		}
60 	}
61 
onRemoval(DownloadManager dm)62 	public static SaveLocationChange onRemoval(DownloadManager dm) {
63 		if (!isApplicableDownload(dm)) {return null;}
64 		try {return CURRENT_HANDLER.onRemoval(PluginCoreUtils.wrap(dm), true, true);}
65 		catch (Exception e) {
66 			logError("Error trying to determine on-removal location.", dm, e);
67 			return null;
68 		}
69 	}
70 
onCompletion(DownloadManager dm, MoveCallback callback )71 	public static SaveLocationChange onCompletion(DownloadManager dm, MoveCallback callback ) {
72 		if (!isApplicableDownload(dm)) {return null;}
73 
74 		if (dm.getDownloadState().getFlag(DownloadManagerState.FLAG_MOVE_ON_COMPLETION_DONE)) {
75 			logInfo("Completion flag already set on " + describe(dm) + ", skip move-on-completion behaviour.", dm);
76 			return null;
77 		}
78 
79 		SaveLocationChange sc;
80 		try {sc = CURRENT_HANDLER.onCompletion(PluginCoreUtils.wrap(dm), true, true);}
81 		catch (Exception e) {
82 			logError("Error trying to determine on-completion location.", dm, e);
83 			return null;
84 		}
85 
86 			// give caller the opportunity to perform the action *before* we set the completion-done
87 			// flag...
88 
89 		if ( callback != null && sc != null ){
90 
91 			callback.perform( sc );
92 		}
93 
94 		logInfo("Setting completion flag on " + describe(dm) + ", may have been set before.", dm);
95 		dm.getDownloadState().setFlag(DownloadManagerState.FLAG_MOVE_ON_COMPLETION_DONE, true);
96 		return sc;
97 	}
98 
canGoToCompleteDir(DownloadManager dm)99 	public static boolean canGoToCompleteDir(DownloadManager dm) {
100 		return (dm.isDownloadComplete(false) && isOnCompleteEnabled());
101 	}
102 
isOnCompleteEnabled()103 	public static boolean isOnCompleteEnabled() {
104 		return COConfigurationManager.getBooleanParameter("Move Completed When Done");
105 	}
106 
isOnRemovalEnabled()107 	public static boolean isOnRemovalEnabled() {
108 		return COConfigurationManager.getBooleanParameter("File.move.download.removed.enabled");
109 	}
110 
recalculatePath(DownloadManager dm)111 	public static SaveLocationChange recalculatePath(DownloadManager dm) {
112 		Download download = PluginCoreUtils.wrap(dm);
113 		SaveLocationChange result = null;
114 		if (canGoToCompleteDir(dm)) {
115 			result = CURRENT_HANDLER.onCompletion(download, true, false);
116 		}
117 		if (result == null) {
118 			result = CURRENT_HANDLER.onInitialization(download, true, false);
119 		}
120 		return result;
121 	}
122 
123 	/**
124 	 * Find all file locations that a download might exist in - this is used
125 	 * to see locate existing files to reuse to prevent downloads being re-added.
126 	 */
getRelatedDirs(DownloadManager dm)127 	public static File[] getRelatedDirs(DownloadManager dm) {
128 		ArrayList result = new ArrayList();
129 		Download d = PluginCoreUtils.wrap(dm);
130 
131 		if (isOnCompleteEnabled()) {
132 			addFile(result, COConfigurationManager.getStringParameter("Completed Files Directory"));
133 			addFile(result, CURRENT_HANDLER.onCompletion(d, false, false));
134 			addFile(result, DownloadManagerDefaultPaths.DEFAULT_HANDLER.onCompletion(d, false, false));
135 		}
136 		if (isOnRemovalEnabled()) {
137 			addFile(result, COConfigurationManager.getStringParameter("File.move.download.removed.path"));
138 			addFile(result, CURRENT_HANDLER.onRemoval(d, false, false));
139 			addFile(result, DownloadManagerDefaultPaths.DEFAULT_HANDLER.onRemoval(d, false, false));
140 		}
141 		return (File[])result.toArray(new File[result.size()]);
142 	}
143 
addFile(ArrayList l, SaveLocationChange slc)144 	private static void addFile(ArrayList l, SaveLocationChange slc) {
145 		if (slc != null) {addFile(l, slc.download_location);}
146 	}
147 
addFile(ArrayList l, File f)148 	private static void addFile(ArrayList l, File f) {
149 		if (f != null && !l.contains(f)) {l.add(f);}
150 	}
151 
addFile(ArrayList l, String s)152 	private static void addFile(ArrayList l, String s) {
153 		if (s != null && s.trim().length()!=0) {addFile(l, new File(s));}
154 	}
155 
156 	public interface
157 	MoveCallback
158 	{
159 		public void
perform( SaveLocationChange details )160 		perform(
161 			SaveLocationChange		details );
162 	}
163 }
164