Search in sources :

Example 11 with VuzeFileHandler

use of com.biglybt.core.vuzefile.VuzeFileHandler in project BiglyBT by BiglySoftware.

the class TorrentOpener method mergeFileIntoTorrentInfo.

/**
 * Creates a TorrentInfo from a file.  Prompts user if the file is invalid,
 * torrent already exists
 *
 * @param sFileName
 * @param sOriginatingLocation
 * @return
 * @since 5.0.0.1
 */
// TODO: i18n
public static boolean mergeFileIntoTorrentInfo(String sFileName, final String sOriginatingLocation, TorrentOpenOptions torrentOptions) {
    TOTorrent torrent = null;
    File torrentFile;
    boolean bDeleteFileOnCancel = false;
    // actually made a copy.
    try {
        if (sFileName.startsWith("file://localhost/")) {
            sFileName = UrlUtils.decode(sFileName.substring(16));
        }
        final File fOriginal = new File(sFileName);
        if (!fOriginal.isFile() || !fOriginal.exists()) {
            UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", fOriginal.toString(), new String[] { UrlUtils.decode(sOriginatingLocation), "Not a File" });
            return false;
        }
        if (fOriginal.length() > TorrentUtils.MAX_TORRENT_FILE_SIZE) {
            UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", fOriginal.toString(), new String[] { UrlUtils.decode(sOriginatingLocation), "Too large to be a torrent" });
            return false;
        }
        torrentFile = TorrentUtils.copyTorrentFileToSaveDir(fOriginal, true);
        bDeleteFileOnCancel = !fOriginal.equals(torrentFile);
    // TODO if the files are still equal, and it isn't in the save
    // dir, we should copy it to a temp file in case something
    // re-writes it.  No need to copy a torrent coming from the
    // downloader though..
    } catch (IOException e1) {
        // Use torrent in wherever it is and hope for the best
        // XXX Should error instead?
        Debug.out(e1);
        torrentFile = new File(sFileName);
    }
    VuzeFileHandler vfh = VuzeFileHandler.getSingleton();
    VuzeFile vf = vfh.loadVuzeFile(torrentFile);
    if (vf != null) {
        vfh.handleFiles(new VuzeFile[] { vf }, VuzeFileComponent.COMP_TYPE_NONE);
        return false;
    }
    if (RSSUtils.isRSSFeed(torrentFile)) {
        boolean done = false;
        try {
            URL url = new URL(sOriginatingLocation);
            UIManager ui_manager = StaticUtilities.getUIManager(10 * 1000);
            if (ui_manager != null) {
                String details = MessageText.getString("subscription.request.add.message", new String[] { sOriginatingLocation });
                long res = ui_manager.showMessageBox("subscription.request.add.title", "!" + details + "!", UIManagerEvent.MT_YES | UIManagerEvent.MT_NO);
                if (res == UIManagerEvent.MT_YES) {
                    SubscriptionManager sm = PluginInitializer.getDefaultInterface().getUtilities().getSubscriptionManager();
                    sm.requestSubscription(url);
                    done = true;
                }
            }
        } catch (Throwable e) {
            Debug.out(e);
        }
        if (done) {
            if (bDeleteFileOnCancel) {
                torrentFile.delete();
            }
            return false;
        }
    }
    // Do a quick check to see if it's a torrent
    if (!TorrentUtil.isFileTorrent(sOriginatingLocation, torrentFile, torrentFile.getName(), !torrentOptions.getHideErrors())) {
        if (bDeleteFileOnCancel) {
            torrentFile.delete();
        }
        return false;
    }
    // Load up the torrent, see it it's real
    try {
        torrent = TorrentUtils.readFromFile(torrentFile, false);
    } catch (final TOTorrentException e) {
        UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", Debug.getStackTrace(e), new String[] { sOriginatingLocation, e.getMessage() });
        if (bDeleteFileOnCancel)
            torrentFile.delete();
        return false;
    }
    if (bDeleteFileOnCancel) {
        torrentOptions.setDeleteFileOnCancel(bDeleteFileOnCancel);
    }
    torrentOptions.sFileName = torrentFile.getAbsolutePath();
    torrentOptions.setTorrent(torrent);
    torrentOptions.sOriginatingLocation = sOriginatingLocation;
    return torrentOptions.getTorrent() != null;
}
Also used : UIManager(com.biglybt.pif.ui.UIManager) IOException(java.io.IOException) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File) VuzeFileHandler(com.biglybt.core.vuzefile.VuzeFileHandler)

Example 12 with VuzeFileHandler

use of com.biglybt.core.vuzefile.VuzeFileHandler in project BiglyBT by BiglySoftware.

the class SearchUtils method importFromClipboard.

private static void importFromClipboard() {
    final Shell shell = Utils.findAnyShell();
    shell.getDisplay().asyncExec(new AERunnable() {

        @Override
        public void runSupport() {
            try {
                Clipboard clipboard = new Clipboard(Display.getDefault());
                String text = (String) clipboard.getContents(TextTransfer.getInstance());
                clipboard.dispose();
                if (text != null) {
                    InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
                    try {
                        VuzeFileHandler vfh = VuzeFileHandler.getSingleton();
                        VuzeFile vf = vfh.loadVuzeFile(is);
                        if (vf != null) {
                            vfh.handleFiles(new VuzeFile[] { vf }, VuzeFileComponent.COMP_TYPE_NONE);
                        }
                    } finally {
                        is.close();
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
    });
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) Shell(org.eclipse.swt.widgets.Shell) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) Clipboard(org.eclipse.swt.dnd.Clipboard) VuzeFileHandler(com.biglybt.core.vuzefile.VuzeFileHandler)

Aggregations

VuzeFileHandler (com.biglybt.core.vuzefile.VuzeFileHandler)12 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)11 File (java.io.File)4 IOException (java.io.IOException)2 URL (java.net.URL)2 Shell (org.eclipse.swt.widgets.Shell)2 CoreRunningListener (com.biglybt.core.CoreRunningListener)1 ParameterListener (com.biglybt.core.config.ParameterListener)1 PlatformSubscriptionsMessenger (com.biglybt.core.messenger.config.PlatformSubscriptionsMessenger)1 SearchException (com.biglybt.core.metasearch.SearchException)1 SearchLoginException (com.biglybt.core.metasearch.SearchLoginException)1 SearchParameter (com.biglybt.core.metasearch.SearchParameter)1 WebEngine (com.biglybt.core.metasearch.impl.web.WebEngine)1 Subscription (com.biglybt.core.subs.Subscription)1 SubscriptionDownloadListener (com.biglybt.core.subs.SubscriptionDownloadListener)1 SubscriptionException (com.biglybt.core.subs.SubscriptionException)1 SubscriptionHistory (com.biglybt.core.subs.SubscriptionHistory)1 SubscriptionResult (com.biglybt.core.subs.SubscriptionResult)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1