Search in sources :

Example 56 with TOTorrent

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

the class TorrentUtil method calculateToolbarStates.

// XXX Don't think *View's need this call anymore.  ToolBarView does it fo them
public static Map<String, Long> calculateToolbarStates(ISelectedContent[] currentContent, String viewID_unused) {
    // System.out.println("calculateToolbarStates(" + currentContent.length + ", " + viewID_unused + " via " + Debug.getCompressedStackTrace());
    /*
		String[] TBKEYS = new String[] {
			"download",
			"play",
			"stream",
			"run",
			"top",
			"up",
			"down",
			"bottom",
			"start",
			"stop",
			"remove"
		};
		*/
    Map<String, Long> mapNewToolbarStates = new HashMap<>();
    String[] itemsNeedingSelection = {};
    String[] itemsNeedingRealDMSelection = { "remove", "top", "bottom", "transcode", "startstop" };
    String[] itemsRequiring1DMwithHash = { "details", "comment", "up", "down" };
    String[] itemsRequiring1DMSelection = {};
    int numSelection = currentContent.length;
    boolean hasSelection = numSelection > 0;
    boolean has1Selection = numSelection == 1;
    for (int i = 0; i < itemsNeedingSelection.length; i++) {
        String itemID = itemsNeedingSelection[i];
        mapNewToolbarStates.put(itemID, hasSelection ? UIToolBarItem.STATE_ENABLED : 0);
    }
    TableView tv = SelectedContentManager.getCurrentlySelectedTableView();
    // not sure why we assume that the existance of any table view
    boolean hasRealDM = tv != null;
    if (!hasRealDM && numSelection > 0) {
        hasRealDM = true;
        for (int i = 0; i < currentContent.length; i++) {
            ISelectedContent content = currentContent[i];
            DownloadManager dm = content.getDownloadManager();
            if (dm == null) {
                hasRealDM = false;
                break;
            }
        }
    }
    if (!hasRealDM) {
        MultipleDocumentInterfaceSWT mdi = UIFunctionsManagerSWT.getUIFunctionsSWT().getMDISWT();
        if (mdi != null) {
            MdiEntrySWT entry = mdi.getCurrentEntrySWT();
            if (entry != null) {
                if (entry.getDatasource() instanceof DownloadManager) {
                    hasRealDM = true;
                } else if ((entry instanceof UIPluginView) && (((UIPluginView) entry).getDataSource() instanceof DownloadManager)) {
                    hasRealDM = true;
                }
            }
        }
    }
    boolean canStart = false;
    boolean canStop = false;
    boolean canRemoveFileInfo = false;
    boolean canRunFileInfo = false;
    boolean canCheckExist = false;
    boolean hasDM = false;
    boolean canRecheck = false;
    if (currentContent.length > 0 && hasRealDM) {
        // well, in fact, we can have hasRealDM set to true here (because tv isn't null) and actually not have a real dm.
        // fancy that - protect against null DownloadManagers...
        boolean canMoveUp = false;
        boolean canMoveDown = false;
        boolean canDownload = false;
        canCheckExist = true;
        GlobalManager gm = null;
        for (int i = 0; i < currentContent.length; i++) {
            ISelectedContent content = currentContent[i];
            DownloadManager dm = content.getDownloadManager();
            if (dm == null) {
                if (!canDownload && content.getDownloadInfo() != null) {
                    canDownload = true;
                }
                continue;
            }
            if (gm == null) {
                gm = dm.getGlobalManager();
            }
            int state = dm.getState();
            canCheckExist &= (state == DownloadManager.STATE_ERROR || state == DownloadManager.STATE_STOPPED || state == DownloadManager.STATE_QUEUED);
            int fileIndex = content.getFileIndex();
            if (fileIndex == -1) {
                if (!canMoveUp && gm.isMoveableUp(dm)) {
                    canMoveUp = true;
                }
                if (!canMoveDown && gm.isMoveableDown(dm)) {
                    canMoveDown = true;
                }
                hasDM = true;
                if (!canStart && ManagerUtils.isStartable(dm)) {
                    canStart = true;
                }
                if (!canStop && ManagerUtils.isStopable(dm)) {
                    canStop = true;
                }
            } else {
                DiskManagerFileInfoSet fileInfos = dm.getDiskManagerFileInfoSet();
                if (fileIndex < fileInfos.nbFiles()) {
                    DiskManagerFileInfo fileInfo = fileInfos.getFiles()[fileIndex];
                    if (!canStart && (fileInfo.isSkipped())) {
                        canStart = true;
                    }
                    if (!canStop && !fileInfo.isSkipped()) {
                        canStop = true;
                    }
                    if (!canRemoveFileInfo && !fileInfo.isSkipped()) {
                        int storageType = fileInfo.getStorageType();
                        if (storageType == DiskManagerFileInfo.ST_LINEAR || storageType == DiskManagerFileInfo.ST_COMPACT) {
                            canRemoveFileInfo = true;
                        }
                    }
                    if (!canRunFileInfo && fileInfo.getAccessMode() == DiskManagerFileInfo.READ && fileInfo.getDownloaded() == fileInfo.getLength() && fileInfo.getFile(true).exists()) {
                        canRunFileInfo = true;
                    }
                }
            }
            canRecheck = canRecheck || dm.canForceRecheck();
        }
        boolean canRemove = hasDM || canRemoveFileInfo;
        mapNewToolbarStates.put("remove", canRemove ? UIToolBarItem.STATE_ENABLED : 0);
        mapNewToolbarStates.put("download", canDownload ? UIToolBarItem.STATE_ENABLED : 0);
        if (currentContent.length == 1) {
            mapNewToolbarStates.put("up", canMoveUp ? UIToolBarItem.STATE_ENABLED : 0);
            mapNewToolbarStates.put("down", canMoveDown ? UIToolBarItem.STATE_ENABLED : 0);
        }
    }
    boolean canRun = has1Selection && ((hasDM && !canRunFileInfo) || (!hasDM && canRunFileInfo));
    if (canRun) {
        ISelectedContent content = currentContent[0];
        DownloadManager dm = content.getDownloadManager();
        if (dm == null) {
            canRun = false;
        } else {
            TOTorrent torrent = dm.getTorrent();
            if (torrent == null) {
                canRun = false;
            } else if (!dm.getAssumedComplete() && torrent.isSimpleTorrent()) {
                canRun = false;
            /*
									} else if (PlatformTorrentUtils.useEMP(torrent)
											&& PlatformTorrentUtils.embeddedPlayerAvail()
											&& PlayUtils.canProgressiveOrIsComplete(torrent)) {
										// play button enabled and not UMP.. don't need launch

										canRun = false;

									}
									*/
            }
        }
    }
    mapNewToolbarStates.put("run", canRun ? UIToolBarItem.STATE_ENABLED : 0);
    mapNewToolbarStates.put("start", canStart ? UIToolBarItem.STATE_ENABLED : 0);
    mapNewToolbarStates.put("stop", canStop ? UIToolBarItem.STATE_ENABLED : 0);
    mapNewToolbarStates.put("startstop", canStart || canStop ? UIToolBarItem.STATE_ENABLED : 0);
    for (int i = 0; i < itemsNeedingRealDMSelection.length; i++) {
        String itemID = itemsNeedingRealDMSelection[i];
        if (!mapNewToolbarStates.containsKey(itemID)) {
            mapNewToolbarStates.put(itemID, hasSelection && hasDM && hasRealDM ? UIToolBarItem.STATE_ENABLED : 0);
        }
    }
    for (int i = 0; i < itemsRequiring1DMSelection.length; i++) {
        String itemID = itemsRequiring1DMSelection[i];
        if (!mapNewToolbarStates.containsKey(itemID)) {
            mapNewToolbarStates.put(itemID, has1Selection && hasDM ? UIToolBarItem.STATE_ENABLED : 0);
        }
    }
    for (int i = 0; i < itemsRequiring1DMwithHash.length; i++) {
        String itemID = itemsRequiring1DMwithHash[i];
        if (!mapNewToolbarStates.containsKey(itemID)) {
            mapNewToolbarStates.put(itemID, hasDM ? UIToolBarItem.STATE_ENABLED : 0);
        }
    }
    mapNewToolbarStates.put("download", has1Selection && (!(currentContent[0] instanceof ISelectedVuzeFileContent)) && currentContent[0].getDownloadManager() == null && (currentContent[0].getHash() != null || currentContent[0].getDownloadInfo() != null) ? UIToolBarItem.STATE_ENABLED : 0);
    if (tv != null) {
        TableColumn tc = tv.getTableColumn(RankItem.COLUMN_ID);
        if (tc != null && !tc.isVisible()) {
            mapNewToolbarStates.put("up", 0L);
            mapNewToolbarStates.put("down", 0L);
        }
    }
    mapNewToolbarStates.put(TU_ITEM_RECHECK, canRecheck ? UIToolBarItem.STATE_ENABLED : 0);
    mapNewToolbarStates.put(TU_ITEM_CHECK_FILES, canCheckExist ? UIToolBarItem.STATE_ENABLED : 0);
    return mapNewToolbarStates;
}
Also used : ISelectedVuzeFileContent(com.biglybt.ui.selectedcontent.ISelectedVuzeFileContent) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) ISelectedContent(com.biglybt.ui.selectedcontent.ISelectedContent) DiskManagerFileInfoSet(com.biglybt.core.disk.DiskManagerFileInfoSet) MdiEntrySWT(com.biglybt.ui.swt.mdi.MdiEntrySWT) DownloadManager(com.biglybt.core.download.DownloadManager) TableColumn(com.biglybt.pif.ui.tables.TableColumn) GlobalManager(com.biglybt.core.global.GlobalManager) TOTorrent(com.biglybt.core.torrent.TOTorrent) UIPluginView(com.biglybt.pif.ui.UIPluginView) MultipleDocumentInterfaceSWT(com.biglybt.ui.swt.mdi.MultipleDocumentInterfaceSWT) TableView(com.biglybt.ui.common.table.TableView)

Example 57 with TOTorrent

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

the class CertificateTrustWindow method trustCertificate.

@Override
public boolean trustCertificate(final String _resource, final X509Certificate cert) {
    final Display display = Utils.getDisplay();
    if (display.isDisposed()) {
        return (false);
    }
    TOTorrent torrent = TorrentUtils.getTLSTorrent();
    final String resource;
    if (torrent != null) {
        resource = TorrentUtils.getLocalisedName(torrent) + "\n" + _resource;
    } else {
        resource = _resource;
    }
    final trustDialog[] dialog = new trustDialog[1];
    try {
        Utils.execSWTThread(new AERunnable() {

            @Override
            public void runSupport() {
                dialog[0] = new trustDialog(display, resource, cert);
            }
        }, false);
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        return (false);
    }
    return (dialog[0].getTrusted());
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 58 with TOTorrent

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

the class MenuFactory method handleTorrentFixup.

private static void handleTorrentFixup() {
    // had some OSX SWT crash issues in this code so moved everything async
    final Shell shell = Utils.findAnyShell();
    try {
        FileDialog dialog = new FileDialog(shell, SWT.SYSTEM_MODAL | SWT.OPEN);
        dialog.setFilterExtensions(new String[] { "*.torrent", "*.tor", Constants.FILE_WILDCARD });
        dialog.setFilterNames(new String[] { "*.torrent", "*.tor", Constants.FILE_WILDCARD });
        dialog.setFilterPath(TorrentOpener.getFilterPathTorrent());
        dialog.setText(MessageText.getString("torrent.fix.corrupt.browse"));
        String str = dialog.open();
        if (str != null) {
            TorrentOpener.setFilterPathTorrent(str);
            File file = new File(str);
            byte[] bytes = FileUtil.readFileAsByteArray(file);
            Map existing_map = BDecoder.decode(bytes);
            Map existing_info = (Map) existing_map.get("info");
            byte[] existing_info_encoded = BEncoder.encode(existing_info);
            final TOTorrent t = TOTorrentFactory.deserialiseFromMap(existing_map);
            final byte[] old_hash = t.getHash();
            byte[] new_hash = null;
            for (int i = 0; i < bytes.length - 5; i++) {
                if (bytes[i] == ':' && bytes[i + 1] == 'i' && bytes[i + 2] == 'n' && bytes[i + 3] == 'f' && bytes[i + 4] == 'o') {
                    new_hash = new SHA1Simple().calculateHash(bytes, i + 5, existing_info_encoded.length);
                    break;
                }
            }
            if (new_hash != null) {
                final byte[] f_new_hash = new_hash;
                Utils.execSWTThreadLater(1, new Runnable() {

                    @Override
                    public void run() {
                        String title = MessageText.getString("torrent.fix.corrupt.result.title");
                        if (Arrays.equals(old_hash, f_new_hash)) {
                            MessageBoxShell mb = new MessageBoxShell(SWT.OK, title, MessageText.getString("torrent.fix.corrupt.result.nothing"));
                            mb.setParent(shell);
                            mb.open(null);
                        } else {
                            MessageBoxShell mb = new MessageBoxShell(SWT.OK, title, MessageText.getString("torrent.fix.corrupt.result.fixed", new String[] { ByteFormatter.encodeString(f_new_hash) }));
                            mb.setParent(shell);
                            mb.open(null);
                            mb.waitUntilClosed();
                            try {
                                t.setHashOverride(f_new_hash);
                                Utils.execSWTThreadLater(1, new Runnable() {

                                    @Override
                                    public void run() {
                                        FileDialog dialog2 = new FileDialog(shell, SWT.SYSTEM_MODAL | SWT.SAVE);
                                        dialog2.setFilterPath(TorrentOpener.getFilterPathTorrent());
                                        dialog2.setFilterExtensions(new String[] { "*.torrent" });
                                        String str2 = dialog2.open();
                                        if (str2 != null) {
                                            if (!(str2.toLowerCase(Locale.US).endsWith(".tor") || str2.toLowerCase(Locale.US).endsWith(".torrent"))) {
                                                str2 += ".torrent";
                                            }
                                            try {
                                                t.serialiseToBEncodedFile(new File(str2));
                                            } catch (Throwable e) {
                                                Debug.out(e);
                                            }
                                        }
                                    }
                                });
                            } catch (Throwable e) {
                                Debug.out(e);
                            }
                        }
                    }
                });
            }
        }
    } catch (Throwable e) {
        Debug.out(e);
    }
}
Also used : MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) TOTorrent(com.biglybt.core.torrent.TOTorrent) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 59 with TOTorrent

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

the class MenuFactory method handleTorrentView.

private static void handleTorrentView() {
    final Shell shell = Utils.findAnyShell();
    try {
        FileDialog dialog = new FileDialog(shell, SWT.SYSTEM_MODAL | SWT.OPEN);
        dialog.setFilterExtensions(new String[] { "*.torrent", "*.tor", Constants.FILE_WILDCARD });
        dialog.setFilterNames(new String[] { "*.torrent", "*.tor", Constants.FILE_WILDCARD });
        dialog.setFilterPath(TorrentOpener.getFilterPathTorrent());
        dialog.setText(MessageText.getString("torrent.fix.corrupt.browse"));
        String str = dialog.open();
        if (str != null) {
            TorrentOpener.setFilterPathTorrent(str);
            File file = new File(str);
            StringBuilder content = new StringBuilder();
            String NL = "\r\n";
            try {
                TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedFile(file);
                LocaleUtilDecoder locale_decoder = LocaleTorrentUtil.getTorrentEncoding(torrent);
                content.append("Character Encoding:\t").append(locale_decoder.getName()).append(NL);
                String display_name = locale_decoder.decodeString(torrent.getName());
                content.append("Name:\t").append(display_name).append(NL);
                byte[] hash = torrent.getHash();
                content.append("Hash:\t").append(ByteFormatter.encodeString(hash)).append(NL);
                content.append("Size:\t").append(DisplayFormatters.formatByteCountToKiBEtc(torrent.getSize())).append(", piece size=").append(DisplayFormatters.formatByteCountToKiBEtc(torrent.getPieceLength())).append(", piece count=").append(torrent.getPieces().length).append(NL);
                if (torrent.getPrivate()) {
                    content.append("Private Torrent").append(NL);
                }
                URL announce_url = torrent.getAnnounceURL();
                if (announce_url != null) {
                    content.append("Announce URL:\t").append(announce_url).append(NL);
                }
                TOTorrentAnnounceURLSet[] sets = torrent.getAnnounceURLGroup().getAnnounceURLSets();
                if (sets.length > 0) {
                    content.append("Announce List").append(NL);
                    for (TOTorrentAnnounceURLSet set : sets) {
                        String x = "";
                        URL[] urls = set.getAnnounceURLs();
                        for (URL u : urls) {
                            x += (x.length() == 0 ? "" : ", ") + u;
                        }
                        content.append("\t").append(x).append(NL);
                    }
                }
                content.append("Magnet URI:\t").append(UrlUtils.getMagnetURI(display_name, PluginCoreUtils.wrap(torrent))).append(NL);
                long c_date = torrent.getCreationDate();
                if (c_date > 0) {
                    content.append("Created On:\t").append(DisplayFormatters.formatDate(c_date * 1000)).append(NL);
                }
                byte[] created_by = torrent.getCreatedBy();
                if (created_by != null) {
                    content.append("Created By:\t").append(locale_decoder.decodeString(created_by)).append(NL);
                }
                byte[] comment = torrent.getComment();
                if (comment != null) {
                    content.append("Comment:\t").append(locale_decoder.decodeString(comment)).append(NL);
                }
                TOTorrentFile[] files = torrent.getFiles();
                content.append("Files:\t").append(files.length).append(" - simple=").append(torrent.isSimpleTorrent()).append(NL);
                for (TOTorrentFile tf : files) {
                    byte[][] comps = tf.getPathComponents();
                    String f_name = "";
                    for (byte[] comp : comps) {
                        f_name += (f_name.length() == 0 ? "" : File.separator) + locale_decoder.decodeString(comp);
                    }
                    content.append("\t").append(f_name).append("\t\t").append(DisplayFormatters.formatByteCountToKiBEtc(tf.getLength())).append(NL);
                }
            } catch (Throwable e) {
                content.append(Debug.getNestedExceptionMessage(e));
            }
            new TextViewerWindow(MessageText.getString("MainWindow.menu.quick_view") + ": " + file.getName(), null, content.toString(), false);
        }
    } catch (Throwable e) {
        Debug.out(e);
    }
}
Also used : URL(java.net.URL) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) LocaleUtilDecoder(com.biglybt.core.internat.LocaleUtilDecoder) TOTorrent(com.biglybt.core.torrent.TOTorrent) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File) TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet)

Example 60 with TOTorrent

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

the class TorrentOpener method openTorrentsForTracking.

protected static void openTorrentsForTracking(final String path, final String[] fileNames) {
    CoreWaiterSWT.waitForCoreRunning(new CoreRunningListener() {

        @Override
        public void coreRunning(final Core core) {
            final Display display = Utils.getDisplay();
            if (display == null || display.isDisposed() || core == null)
                return;
            new AEThread2("TorrentOpener") {

                @Override
                public void run() {
                    for (int i = 0; i < fileNames.length; i++) {
                        try {
                            TOTorrent t = TorrentUtils.readFromFile(new File(path, fileNames[i]), true);
                            core.getTrackerHost().hostTorrent(t, true, true);
                        } catch (Throwable e) {
                            Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Torrent open fails for '" + path + File.separator + fileNames[i] + "'", e));
                        }
                    }
                }
            }.start();
        }
    });
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) CoreRunningListener(com.biglybt.core.CoreRunningListener) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File) LogAlert(com.biglybt.core.logging.LogAlert) Core(com.biglybt.core.Core) Display(org.eclipse.swt.widgets.Display)

Aggregations

TOTorrent (com.biglybt.core.torrent.TOTorrent)123 DownloadManager (com.biglybt.core.download.DownloadManager)34 File (java.io.File)32 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)29 URL (java.net.URL)25 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)15 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)13 IOException (java.io.IOException)13 GlobalManager (com.biglybt.core.global.GlobalManager)10 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)10 Torrent (com.biglybt.pif.torrent.Torrent)9 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)8 TOTorrentAnnounceURLSet (com.biglybt.core.torrent.TOTorrentAnnounceURLSet)8 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)8 List (java.util.List)8 Map (java.util.Map)7 Core (com.biglybt.core.Core)6 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)6 Download (com.biglybt.pif.download.Download)6 TorrentImpl (com.biglybt.pifimpl.local.torrent.TorrentImpl)6