Search in sources :

Example 6 with TorrentOpenOptions

use of com.biglybt.core.torrent.impl.TorrentOpenOptions in project BiglyBT by BiglySoftware.

the class TorrentOpener method openTorrentsFromStrings.

/**
 * Adds torrents that are listed in torrents array.  torrent array can
 * can contain urls or file names.  File names get pathPrefix appended.
 * <P>
 * will open url download dialog, or warning dialogs
 *
 * @since 5.0.0.1
 */
public static void openTorrentsFromStrings(TorrentOpenOptions optionsToClone, Shell parent, String pathPrefix, String[] torrents, String referrer, TorrentDownloaderCallBackInterface listener, boolean forceTorrentOptionsWindow) {
    // if no torrents, but pathPrefix is directory, collect all torrents in it
    if (torrents == null || torrents.length == 0) {
        if (pathPrefix == null) {
            return;
        }
        File path = new File(pathPrefix);
        if (!path.isDirectory()) {
            return;
        }
        List<String> newTorrents = new ArrayList<>();
        File[] listFiles = path.listFiles();
        for (File file : listFiles) {
            try {
                if (file.isFile() && TorrentUtils.isTorrentFile(file.getAbsolutePath())) {
                    newTorrents.add(file.getName());
                }
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }
        }
        if (newTorrents.size() == 0) {
            return;
        }
        torrents = newTorrents.toArray(new String[0]);
    }
    // trim out any .vuze files
    final VuzeFileHandler vfh = VuzeFileHandler.getSingleton();
    List<VuzeFile> vuze_files = new ArrayList<>();
    for (String line : torrents) {
        line = line.trim();
        if (line.startsWith("\"") && line.endsWith("\"")) {
            if (line.length() < 3) {
                line = "";
            } else {
                line = line.substring(1, line.length() - 2);
            }
        }
        TorrentOpenOptions torrentOptions = optionsToClone == null ? new TorrentOpenOptions() : new TorrentOpenOptions(optionsToClone);
        File file = pathPrefix == null ? new File(line) : new File(pathPrefix, line);
        if (file.exists()) {
            try {
                VuzeFile vf = vfh.loadVuzeFile(file);
                if (vf != null) {
                    vuze_files.add(vf);
                    continue;
                }
            } catch (Throwable e) {
                Debug.printStackTrace(e);
            }
            try {
                File torrentDir = new File(COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory"));
                if (COConfigurationManager.getBooleanParameter("Save Torrent Files") && torrentDir.exists() && file.getParentFile().equals(torrentDir)) {
                    torrentOptions.setDeleteFileOnCancel(false);
                }
            } catch (Throwable e) {
            }
            UIFunctions uif = UIFunctionsManager.getUIFunctions();
            if (TorrentOpener.mergeFileIntoTorrentInfo(file.getAbsolutePath(), null, torrentOptions)) {
                uif.addTorrentWithOptions(forceTorrentOptionsWindow, torrentOptions);
            }
            continue;
        }
        final String url = UrlUtils.parseTextForURL(line, true);
        if (url != null) {
            // we used to load any URL, but that results in double loading..
            if (VuzeFileHandler.isAcceptedVuzeFileName(url)) {
                new AEThread2("VuzeLoader") {

                    @Override
                    public void run() {
                        try {
                            // XXX This takes a while..
                            VuzeFile vf = vfh.loadVuzeFile(url);
                            if (vf != null) {
                                vfh.handleFiles(new VuzeFile[] { vf }, VuzeFileComponent.COMP_TYPE_NONE);
                            }
                        } catch (Throwable e) {
                            Debug.printStackTrace(e);
                        }
                    }
                }.start();
                continue;
            }
            new FileDownloadWindow(parent, url, referrer, null, torrentOptions, listener);
        }
    }
    if (vuze_files.size() > 0) {
        VuzeFile[] vfs = new VuzeFile[vuze_files.size()];
        vuze_files.toArray(vfs);
        vfh.handleFiles(vfs, VuzeFileComponent.COMP_TYPE_NONE);
    }
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) TorrentOpenOptions(com.biglybt.core.torrent.impl.TorrentOpenOptions) UIFunctions(com.biglybt.ui.UIFunctions) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File) VuzeFileHandler(com.biglybt.core.vuzefile.VuzeFileHandler)

Example 7 with TorrentOpenOptions

use of com.biglybt.core.torrent.impl.TorrentOpenOptions in project BiglyBT by BiglySoftware.

the class UIFunctionsImpl method openTorrentOpenOptions.

@Override
public void openTorrentOpenOptions(Shell shell, String sPathOfFilesToOpen, String[] sFilesToOpen, Map<String, Object> options) {
    Boolean _defaultToStopped = (Boolean) options.get(UIFunctions.OTO_DEFAULT_TO_STOPPED);
    boolean defaultToStopped = _defaultToStopped != null ? _defaultToStopped : UIFunctions.OTO_DEFAULT_TO_STOPPED_DEFAULT;
    Boolean _hideErrors = (Boolean) options.get(UIFunctions.OTO_HIDE_ERRORS);
    boolean hideErrors = _hideErrors != null ? _hideErrors : UIFunctions.OTO_HIDE_ERRORS_DEFAULT;
    TorrentOpenOptions torrentOptions = new TorrentOpenOptions();
    if (defaultToStopped) {
        torrentOptions.setStartMode(TorrentOpenOptions.STARTMODE_STOPPED);
    }
    torrentOptions.setHideErrors(hideErrors);
    if (sFilesToOpen == null) {
        new OpenTorrentWindow(shell);
    } else {
        // with no listener, Downloader will open options window if user configured
        Boolean _forceOpen = (Boolean) options.get(UIFunctions.OTO_FORCE_OPEN);
        boolean forceOpen = _forceOpen != null ? _forceOpen : UIFunctions.OTO_FORCE_OPEN_DEFAULT;
        TorrentOpener.openTorrentsFromStrings(torrentOptions, shell, sPathOfFilesToOpen, sFilesToOpen, null, null, forceOpen);
    }
}
Also used : TorrentOpenOptions(com.biglybt.core.torrent.impl.TorrentOpenOptions) OpenTorrentWindow(com.biglybt.ui.swt.shells.opentorrent.OpenTorrentWindow)

Example 8 with TorrentOpenOptions

use of com.biglybt.core.torrent.impl.TorrentOpenOptions in project BiglyBT by BiglySoftware.

the class OpenTorrentWindow method openTorrent.

protected void openTorrent(String text, String newReferrer) {
    if (newReferrer != null && newReferrer.length() > 0) {
        if (!referrers.contains(newReferrer)) {
            referrers.add(newReferrer);
            COConfigurationManager.setParameter("url_open_referrers", referrers);
            COConfigurationManager.save();
        }
        COConfigurationManager.setParameter(CONFIG_REFERRER_DEFAULT, newReferrer);
        COConfigurationManager.save();
    }
    final String[] splitters = { "\r\n", "\n", "\r", "\t" };
    String[] lines = null;
    for (int i = 0; i < splitters.length; i++) {
        if (text.contains(splitters[i])) {
            lines = text.split(splitters[i]);
            break;
        }
    }
    if (lines == null) {
        lines = new String[] { text };
    }
    TorrentOpener.openTorrentsFromStrings(new TorrentOpenOptions(), parent, null, lines, newReferrer, this, false);
}
Also used : TorrentOpenOptions(com.biglybt.core.torrent.impl.TorrentOpenOptions)

Example 9 with TorrentOpenOptions

use of com.biglybt.core.torrent.impl.TorrentOpenOptions in project BiglyBT by BiglySoftware.

the class FileDownloadWindow method update.

private void update() {
    int localLastState;
    int state;
    synchronized (this) {
        localLastState = lastState;
        state = downloader.getDownloadState();
        // update lastState now, so if we are called again while in this method, we
        // have the right lastState
        lastState = state;
    }
    int percentDone = downloader.getPercentDone();
    IProgressReport pReport = pReporter.getProgressReport();
    switch(state) {
        case TorrentDownloader.STATE_CANCELLED:
            if (localLastState == state) {
                return;
            }
            if (!pReport.isCanceled()) {
                pReporter.cancel();
            }
            return;
        case TorrentDownloader.STATE_DOWNLOADING:
            pReporter.setPercentage(percentDone, downloader.getStatus());
            break;
        case TorrentDownloader.STATE_ERROR:
            if (localLastState == state) {
                return;
            }
            /*
				 * If the user has canceled then a call  to downloader.cancel() has already been made
				 * so don't bother prompting for the user to retry
				 */
            if (pReport.isCanceled()) {
                return;
            }
            if (torrentOptions != null && torrentOptions.getHideErrors()) {
                pReporter.setCancelCloses(true);
                pReporter.cancel();
            } else {
                pReporter.setErrorMessage(MessageText.getString("fileDownloadWindow.state_error") + downloader.getError());
            }
            if (callOnError != null) {
                callOnError.run();
            }
            return;
        case TorrentDownloader.STATE_FINISHED:
            if (localLastState == state) {
                return;
            }
            pReporter.setDone();
            /*
				 * If the listener is present then it handle finishing up; otherwise open the torrent that
				 * was just downloaded
				 */
            if (listener == null) {
                if (torrentOptions == null) {
                    torrentOptions = new TorrentOpenOptions();
                }
                if (TorrentOpener.mergeFileIntoTorrentInfo(downloader.getFile().getAbsolutePath(), original_url, torrentOptions)) {
                    UIFunctionsManager.getUIFunctions().addTorrentWithOptions(force_dialog, torrentOptions);
                }
            }
            return;
        default:
    }
}
Also used : TorrentOpenOptions(com.biglybt.core.torrent.impl.TorrentOpenOptions)

Aggregations

TorrentOpenOptions (com.biglybt.core.torrent.impl.TorrentOpenOptions)9 UIFunctions (com.biglybt.ui.UIFunctions)4 File (java.io.File)3 TOTorrent (com.biglybt.core.torrent.TOTorrent)2 URL (java.net.URL)2 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)1 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 GlobalManager (com.biglybt.core.global.GlobalManager)1 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)1 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)1 VuzeFileHandler (com.biglybt.core.vuzefile.VuzeFileHandler)1 PluginException (com.biglybt.pif.PluginException)1 PluginInterface (com.biglybt.pif.PluginInterface)1 DiskManagerChannel (com.biglybt.pif.disk.DiskManagerChannel)1 DiskManagerEvent (com.biglybt.pif.disk.DiskManagerEvent)1 DiskManagerListener (com.biglybt.pif.disk.DiskManagerListener)1 DiskManagerRequest (com.biglybt.pif.disk.DiskManagerRequest)1 ShareResourceFile (com.biglybt.pif.sharing.ShareResourceFile)1 TrackerWebPageRequest (com.biglybt.pif.tracker.web.TrackerWebPageRequest)1