Search in sources :

Example 1 with GlobalManagerListener

use of com.biglybt.core.global.GlobalManagerListener in project BiglyBT by BiglySoftware.

the class TorrentUIUtilsV3 method _loadTorrent.

private static void _loadTorrent(final Core core, final DownloadUrlInfo dlInfo, // open player
final boolean playNow, // as for open player but don't actually open it
final boolean playPrepare, final boolean bringToFront) {
    if (dlInfo instanceof DownloadUrlInfoSWT) {
        DownloadUrlInfoSWT dlInfoSWT = (DownloadUrlInfoSWT) dlInfo;
        dlInfoSWT.invoke(playNow ? "play" : "download");
        return;
    }
    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) {

                        @Override
                        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;
            }
        }
        UIFunctionsSWT uiFunctions = (UIFunctionsSWT) UIFunctionsManager.getUIFunctions();
        if (uiFunctions != null) {
            // if (!COConfigurationManager.getBooleanParameter("add_torrents_silently")) { not used 11/30/2015
            if (bringToFront) {
                uiFunctions.bringToFront();
            }
            // }
            Shell shell = uiFunctions.getMainShell();
            if (shell != null) {
                new FileDownloadWindow(shell, url, dlInfo.getReferer(), dlInfo.getRequestProperties(), null, new TorrentDownloaderCallBackInterface() {

                    @Override
                    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(dlInfo.getDownloadURL(), file, file.getName(), true)) {
                                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() {

                                @Override
                                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);
                            TorrentOpener.openTorrent(file.getAbsolutePath());
                        }
                    }
                });
            }
        }
    } catch (Exception e) {
        Debug.out(e);
    }
}
Also used : TorrentDownloaderCallBackInterface(com.biglybt.core.torrentdownloader.TorrentDownloaderCallBackInterface) Matcher(java.util.regex.Matcher) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) DownloadManager(com.biglybt.core.download.DownloadManager) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) DownloadUrlInfoSWT(com.biglybt.ui.swt.browser.listener.DownloadUrlInfoSWT) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) Shell(org.eclipse.swt.widgets.Shell) GlobalManager(com.biglybt.core.global.GlobalManager) GlobalManagerAdapter(com.biglybt.core.global.GlobalManagerAdapter) TOTorrent(com.biglybt.core.torrent.TOTorrent) GlobalManagerListener(com.biglybt.core.global.GlobalManagerListener) TorrentDownloader(com.biglybt.core.torrentdownloader.TorrentDownloader) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File)

Aggregations

DownloadManager (com.biglybt.core.download.DownloadManager)1 GlobalManager (com.biglybt.core.global.GlobalManager)1 GlobalManagerAdapter (com.biglybt.core.global.GlobalManagerAdapter)1 GlobalManagerListener (com.biglybt.core.global.GlobalManagerListener)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)1 TorrentDownloader (com.biglybt.core.torrentdownloader.TorrentDownloader)1 TorrentDownloaderCallBackInterface (com.biglybt.core.torrentdownloader.TorrentDownloaderCallBackInterface)1 DownloadUrlInfoSWT (com.biglybt.ui.swt.browser.listener.DownloadUrlInfoSWT)1 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)1 File (java.io.File)1 Matcher (java.util.regex.Matcher)1 Shell (org.eclipse.swt.widgets.Shell)1