String url = dlInfo.getDownloadURL();
try {
Matcher m = hashPattern.matcher(url);
if (m.find()) {
String hash = m.group(1);
GlobalManager gm = core.getGlobalManager();
final DownloadManager dm = gm.getDownloadManager(new HashWrapper(
Base32.decode(hash)));
if (dm != null) {
if (playNow || playPrepare) {
new AEThread2("playExisting", true) {
public void run() {
if (playNow) {
Debug.outNoStack("loadTorrent already exists.. playing",
false);
TorrentListViewsUtils.playOrStream(dm, -1);
} else {
Debug.outNoStack("loadTorrent already exists.. preparing",
false);
PlayUtils.prepareForPlay(dm);
}
}
}.start();
} else {
new MessageBoxShell(SWT.OK,
MSG_ALREADY_EXISTS, new String[] {
" ",
dm.getDisplayName(),
MessageText.getString(MSG_ALREADY_EXISTS_NAME),
}).open(null);
}
return;
}
}
// If it's going to our URLs, add some extra authenication
if (UrlFilter.getInstance().urlCanRPC(url)) {
ContentNetwork cn = null;
if (dlInfo instanceof DownloadUrlInfoContentNetwork) {
cn = ((DownloadUrlInfoContentNetwork) dlInfo).getContentNetwork();
}
if (cn == null) {
cn = ConstantsVuze.getDefaultContentNetwork();
}
url = cn.appendURLSuffix(url, false, true);
}
UIFunctionsSWT uiFunctions = (UIFunctionsSWT) UIFunctionsManager.getUIFunctions();
if (uiFunctions != null) {
if (!COConfigurationManager.getBooleanParameter("add_torrents_silently")) {
if (bringToFront) {
uiFunctions.bringToFront();
}
}
Shell shell = uiFunctions.getMainShell();
if (shell != null) {
new FileDownloadWindow(shell, url, dlInfo.getReferer(),
dlInfo.getRequestProperties(),
new TorrentDownloaderCallBackInterface() {
public void TorrentDownloaderEvent(int state,
TorrentDownloader inf) {
if (state == TorrentDownloader.STATE_FINISHED) {
File file = inf.getFile();
file.deleteOnExit();
// Do a quick check to see if it's a torrent
if (!TorrentUtil.isFileTorrent(file, Utils.findAnyShell(),
file.getName())) {
return;
}
TOTorrent torrent;
try {
torrent = TorrentUtils.readFromFile(file, false);
} catch (TOTorrentException e) {
Debug.out(e);
return;
}
// Security: Only allow torrents from whitelisted trackers
if (playNow
&& !PlatformTorrentUtils.isPlatformTracker(torrent)) {
Debug.out("stopped loading torrent because it's not in whitelist");
return;
}
HashWrapper hw;
try {
hw = torrent.getHashWrapper();
} catch (TOTorrentException e1) {
Debug.out(e1);
return;
}
GlobalManager gm = core.getGlobalManager();
if (playNow || playPrepare) {
DownloadManager existingDM = gm.getDownloadManager(hw);
if (existingDM != null) {
if (playNow) {
TorrentListViewsUtils.playOrStream(existingDM, -1);
} else {
PlayUtils.prepareForPlay(existingDM);
}
return;
}
}
final HashWrapper fhw = hw;
GlobalManagerListener l = new GlobalManagerAdapter() {
public void downloadManagerAdded(DownloadManager dm) {
try {
core.getGlobalManager().removeListener(this);
handleDMAdded(dm, playNow, playPrepare, fhw);
} catch (Exception e) {
Debug.out(e);
}
}
};
gm.addListener(l, false);
if (playNow || playPrepare) {
PlayNowList.add(hw);
}