Search in sources :

Example 51 with TOTorrent

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

the class BuddyPluginUtils method getChatKey.

public static String getChatKey(Torrent torrent) {
    if (torrent == null) {
        return (null);
    }
    // use torrent name here to canonicalize things in case user has renamed download display name
    // also it is more important to get a consistent string rather than something correctly localised
    String torrent_name = null;
    try {
        TOTorrent to_torrent = PluginCoreUtils.unwrap(torrent);
        torrent_name = to_torrent.getUTF8Name();
        if (torrent_name == null) {
            torrent_name = new String(to_torrent.getName(), "UTF-8");
        }
    } catch (Throwable e) {
    }
    if (torrent_name == null) {
        torrent_name = torrent.getName();
    }
    String key = "Download: " + torrent_name + " {" + ByteFormatter.encodeString(torrent.getHash()) + "}";
    return (key);
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 52 with TOTorrent

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

the class TorrentMenuFancy method buildTorrentCustomMenu_Content.

protected void buildTorrentCustomMenu_Content(final Composite detailArea, final DownloadManager[] dms) {
    // Run Data File
    if (hasSelection) {
        createRow(detailArea, "MyTorrentsView.menu.open", "run", new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager[] dms) {
                TorrentUtil.runDataSources(dms);
            }
        });
    }
    // Explore (or open containing folder)
    if (hasSelection) {
        final boolean use_open_containing_folder = COConfigurationManager.getBooleanParameter("MyTorrentsView.menu.show_parent_folder_enabled");
        createRow(detailArea, "MyTorrentsView.menu." + (use_open_containing_folder ? "open_parent_folder" : "explore"), null, new ListenerDMTask(dms, false) {

            @Override
            public void run(DownloadManager dm) {
                ManagerUtils.open(dm, use_open_containing_folder);
            }
        });
    }
    if (hasSelection) {
        createMenuRow(detailArea, "MyTorrentsView.menu.browse", null, new FancyMenuRowInfoListener() {

            @Override
            public void buildMenu(Menu menuBrowse) {
                final MenuItem itemBrowsePublic = new MenuItem(menuBrowse, SWT.PUSH);
                itemBrowsePublic.setText(MessageText.getString("label.public") + "...");
                itemBrowsePublic.addListener(SWT.Selection, new ListenerDMTask(dms, false) {

                    @Override
                    public void run(DownloadManager dm) {
                        ManagerUtils.browse(dm, false, true);
                    }
                });
                final MenuItem itemBrowseAnon = new MenuItem(menuBrowse, SWT.PUSH);
                itemBrowseAnon.setText(MessageText.getString("label.anon") + "...");
                itemBrowseAnon.addListener(SWT.Selection, new ListenerDMTask(dms, false) {

                    @Override
                    public void run(DownloadManager dm) {
                        ManagerUtils.browse(dm, true, true);
                    }
                });
                new MenuItem(menuBrowse, SWT.SEPARATOR);
                final MenuItem itemBrowseURL = new MenuItem(menuBrowse, SWT.PUSH);
                Messages.setLanguageText(itemBrowseURL, "label.copy.url.to.clip");
                itemBrowseURL.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event event) {
                        Utils.getOffOfSWTThread(new AERunnable() {

                            @Override
                            public void runSupport() {
                                String url = ManagerUtils.browse(dms[0], true, false);
                                if (url != null) {
                                    ClipboardCopy.copyToClipBoard(url);
                                }
                            }
                        });
                    }
                });
                itemBrowseURL.setEnabled(dms.length == 1);
                new MenuItem(menuBrowse, SWT.SEPARATOR);
                final MenuItem itemBrowseDir = new MenuItem(menuBrowse, SWT.CHECK);
                Messages.setLanguageText(itemBrowseDir, "library.launch.web.in.browser.dir.list");
                itemBrowseDir.setSelection(COConfigurationManager.getBooleanParameter("Library.LaunchWebsiteInBrowserDirList"));
                itemBrowseDir.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event event) {
                        COConfigurationManager.setParameter("Library.LaunchWebsiteInBrowserDirList", itemBrowseDir.getSelection());
                    }
                });
            }
        });
    }
    // set thumbnail
    createRow(detailArea, "MyTorrentsView.menu.torrent.set.thumb", null, new ListenerDMTask(dms) {

        @Override
        public void run(DownloadManager[] dms) {
            FileDialog fDialog = new FileDialog(parentShell, SWT.OPEN | SWT.MULTI);
            fDialog.setText(MessageText.getString("MainWindow.dialog.choose.thumb"));
            String path = fDialog.open();
            if (path == null)
                return;
            File file = new File(path);
            try {
                byte[] thumbnail = FileUtil.readFileAsByteArray(file);
                String name = file.getName();
                int pos = name.lastIndexOf(".");
                String ext;
                if (pos != -1) {
                    ext = name.substring(pos + 1);
                } else {
                    ext = "";
                }
                String type = HTTPUtils.guessContentTypeFromFileType(ext);
                for (DownloadManager dm : dms) {
                    try {
                        TOTorrent torrent = dm.getTorrent();
                        PlatformTorrentUtils.setContentThumbnail(torrent, thumbnail, type);
                    } catch (Throwable e) {
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
    });
    boolean fileMove = true;
    boolean locateFiles = false;
    boolean exportFiles = true;
    boolean canSetMOC = dms.length > 0;
    boolean canClearMOC = false;
    for (int i = 0; i < dms.length; i++) {
        DownloadManager dm = dms[i];
        if (!dm.canMoveDataFiles()) {
            fileMove = false;
        }
        if (!dm.canExportDownload()) {
            exportFiles = false;
        }
        if (!dm.isDownloadComplete(false)) {
            locateFiles = true;
        }
        boolean incomplete = !dm.isDownloadComplete(true);
        DownloadManagerState dm_state = dm.getDownloadState();
        String moc_dir = dm_state.getAttribute(DownloadManagerState.AT_MOVE_ON_COMPLETE_DIR);
        canSetMOC &= incomplete;
        canClearMOC |= (moc_dir != null && moc_dir.length() > 0);
    }
    if (fileMove) {
        createRow(detailArea, "MyTorrentsView.menu.movedata", null, new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager[] dms) {
                TorrentUtil.moveDataFiles(parentShell, dms);
            }
        });
    }
    if (canSetMOC || canClearMOC) {
        boolean f_canSetMOC = canSetMOC;
        boolean f_canClearMOC = canClearMOC;
        createMenuRow(detailArea, "label.move.on.comp", null, new FancyMenuRowInfoListener() {

            @Override
            public void buildMenu(Menu moc_menu) {
                MenuItem clear_item = new MenuItem(moc_menu, SWT.PUSH);
                Messages.setLanguageText(clear_item, "Button.clear");
                clear_item.addListener(SWT.Selection, new ListenerDMTask(dms) {

                    @Override
                    public void run(DownloadManager[] dms) {
                        TorrentUtil.clearMOC(dms);
                    }
                });
                clear_item.setEnabled(f_canClearMOC);
                MenuItem set_item = new MenuItem(moc_menu, SWT.PUSH);
                Messages.setLanguageText(set_item, "label.set");
                set_item.addListener(SWT.Selection, new ListenerDMTask(dms) {

                    @Override
                    public void run(DownloadManager[] dms) {
                        TorrentUtil.setMOC(parentShell, dms);
                    }
                });
                set_item.setEnabled(f_canSetMOC);
            }
        });
    }
    if (exportFiles) {
        createRow(detailArea, "MyTorrentsView.menu.exportdownload", null, new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager[] dms) {
                TorrentUtil.exportDownloads(parentShell, dms);
            }
        });
    }
    createRow(detailArea, "MyTorrentsView.menu.checkfilesexist", null, new ListenerDMTask(dms) {

        @Override
        public void run(DownloadManager dm) {
            dm.filesExist(true);
        }
    });
    if (locateFiles) {
        createRow(detailArea, "MyTorrentsView.menu.locatefiles", null, new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager[] dms) {
                ManagerUtils.locateFiles(dms, parentShell);
            }
        });
    }
    if (dms.length == 1 && ManagerUtils.canFindMoreLikeThis()) {
        createRow(detailArea, "MyTorrentsView.menu.findmorelikethis", null, new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager[] dms) {
                ManagerUtils.findMoreLikeThis(dms[0], parentShell);
            }
        });
    }
    createRow(detailArea, "MyTorrentsView.menu.thisColumn.toClipboard", null, new Listener() {

        @Override
        public void handleEvent(Event event) {
            String sToClipboard = "";
            if (column == null) {
                return;
            }
            String columnName = column.getName();
            if (columnName == null) {
                return;
            }
            TableRowCore[] rows = tv.getSelectedRows();
            for (TableRowCore row : rows) {
                if (row != rows[0]) {
                    sToClipboard += "\n";
                }
                TableCellCore cell = row.getTableCellCore(columnName);
                if (cell != null) {
                    sToClipboard += cell.getClipboardText();
                }
            }
            if (sToClipboard.length() == 0) {
                return;
            }
            new Clipboard(Display.getDefault()).setContents(new Object[] { sToClipboard }, new Transfer[] { TextTransfer.getInstance() });
        }
    });
}
Also used : TableSelectedRowsListener(com.biglybt.ui.swt.views.table.TableSelectedRowsListener) MenuItem(com.biglybt.pif.ui.menus.MenuItem) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) Point(org.eclipse.swt.graphics.Point) TableRowCore(com.biglybt.ui.common.table.TableRowCore) TOTorrent(com.biglybt.core.torrent.TOTorrent) TableCellCore(com.biglybt.ui.common.table.TableCellCore) Transfer(org.eclipse.swt.dnd.Transfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Clipboard(org.eclipse.swt.dnd.Clipboard) File(java.io.File)

Example 53 with TOTorrent

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

the class TorrentUtil method addTrackerTorrentMenu.

protected static void addTrackerTorrentMenu(final Menu menuTracker, final DownloadManager[] dms, boolean changeUrl, boolean manualUpdate, boolean allStopped, final boolean use_open_containing_folder, boolean canMove) {
    Shell shell = Utils.findAnyShell();
    boolean hasSelection = dms.length > 0;
    final MenuItem itemChangeTracker = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemChangeTracker, "MyTorrentsView.menu.changeTracker");
    Utils.setMenuItemImage(itemChangeTracker, "add_tracker");
    itemChangeTracker.addListener(SWT.Selection, new ListenerDMTask(dms) {

        @Override
        public void run(DownloadManager[] dms) {
            if (dms.length > 0) {
                new TrackerChangerWindow(dms);
            }
        }
    });
    itemChangeTracker.setEnabled(changeUrl);
    // edit tracker URLs
    final MenuItem itemEditTracker = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemEditTracker, "MyTorrentsView.menu.editTracker");
    Utils.setMenuItemImage(itemEditTracker, "edit_trackers");
    itemEditTracker.addListener(SWT.Selection, new ListenerDMTask(dms) {

        @Override
        public void run(final DownloadManager[] dms) {
            Map<String, List<DownloadManager>> same_map = new HashMap<>();
            for (DownloadManager dm : dms) {
                TOTorrent torrent = dm.getTorrent();
                if (torrent == null) {
                    continue;
                }
                List<List<String>> group = TorrentUtils.announceGroupsToList(torrent);
                String str = "";
                for (List<String> l : group) {
                    str += "[[";
                    for (String s : l) {
                        str += s + ", ";
                    }
                }
                List<DownloadManager> dl = same_map.get(str);
                if (dl == null) {
                    dl = new ArrayList<>();
                    same_map.put(str, dl);
                }
                dl.add(dm);
            }
            for (final List<DownloadManager> set : same_map.values()) {
                TOTorrent torrent = set.get(0).getTorrent();
                List<List<String>> group = TorrentUtils.announceGroupsToList(torrent);
                new MultiTrackerEditor(null, null, group, new TrackerEditorListener() {

                    @Override
                    public void trackersChanged(String str, String str2, List<List<String>> group) {
                        for (DownloadManager dm : set) {
                            TOTorrent torrent = dm.getTorrent();
                            TorrentUtils.listToAnnounceGroups(group, torrent);
                            try {
                                TorrentUtils.writeToFile(torrent);
                            } catch (Throwable e) {
                                Debug.printStackTrace(e);
                            }
                            if (dm.getTrackerClient() != null) {
                                dm.getTrackerClient().resetTrackerUrl(true);
                            }
                        }
                    }
                }, true, true);
            }
        }
    });
    itemEditTracker.setEnabled(hasSelection);
    // edit tracker URLs together
    final MenuItem itemEditTrackerMerged = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemEditTrackerMerged, "MyTorrentsView.menu.editTrackerMerge");
    itemEditTrackerMerged.addListener(SWT.Selection, new ListenerDMTask(dms) {

        @Override
        public void run(final DownloadManager[] dms) {
            final List<List<String>> merged_trackers = new ArrayList<>();
            Set<String> added = new HashSet<>();
            for (DownloadManager dm : dms) {
                TOTorrent torrent = dm.getTorrent();
                if (torrent == null) {
                    continue;
                }
                List<List<String>> group = TorrentUtils.announceGroupsToList(torrent);
                for (List<String> set : group) {
                    List<String> rem = new ArrayList<>();
                    for (String url_str : set) {
                        try {
                            URL url = new URL(url_str);
                            if (TorrentUtils.isDecentralised(url)) {
                                continue;
                            }
                            if (!added.contains(url_str)) {
                                added.add(url_str);
                                rem.add(url_str);
                            }
                        } catch (Throwable e) {
                        }
                    }
                    if (rem.size() > 0) {
                        merged_trackers.add(rem);
                    }
                }
            }
            new MultiTrackerEditor(null, null, merged_trackers, new TrackerEditorListener() {

                @Override
                public void trackersChanged(String str, String str2, List<List<String>> group) {
                    for (DownloadManager dm : dms) {
                        TOTorrent torrent = dm.getTorrent();
                        TorrentUtils.listToAnnounceGroups(group, torrent);
                        try {
                            TorrentUtils.writeToFile(torrent);
                        } catch (Throwable e) {
                            Debug.printStackTrace(e);
                        }
                        if (dm.getTrackerClient() != null) {
                            dm.getTrackerClient().resetTrackerUrl(true);
                        }
                    }
                }
            }, true, true);
        }
    });
    itemEditTrackerMerged.setEnabled(dms.length > 1);
    // edit webseeds
    final MenuItem itemEditWebSeeds = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemEditWebSeeds, "MyTorrentsView.menu.editWebSeeds");
    itemEditWebSeeds.addListener(SWT.Selection, new ListenerDMTask(dms) {

        @Override
        public void run(final DownloadManager[] dms) {
            final TOTorrent torrent = dms[0].getTorrent();
            if (torrent == null) {
                return;
            }
            List getright = getURLList(torrent, "url-list");
            List webseeds = getURLList(torrent, "httpseeds");
            Map ws = new HashMap();
            ws.put("getright", getright);
            ws.put("webseeds", webseeds);
            ws = BDecoder.decodeStrings(ws);
            new WebSeedsEditor(null, ws, new WebSeedsEditorListener() {

                @Override
                public void webSeedsChanged(String oldName, String newName, Map ws) {
                    try {
                        // String -> byte[]
                        ws = BDecoder.decode(BEncoder.encode(ws));
                        List getright = (List) ws.get("getright");
                        if (getright == null || getright.size() == 0) {
                            torrent.removeAdditionalProperty("url-list");
                        } else {
                            torrent.setAdditionalListProperty("url-list", getright);
                        }
                        List webseeds = (List) ws.get("webseeds");
                        if (webseeds == null || webseeds.size() == 0) {
                            torrent.removeAdditionalProperty("httpseeds");
                        } else {
                            torrent.setAdditionalListProperty("httpseeds", webseeds);
                        }
                        PluginInterface pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass(ExternalSeedPlugin.class);
                        if (pi != null) {
                            ExternalSeedPlugin ext_seed_plugin = (ExternalSeedPlugin) pi.getPlugin();
                            ext_seed_plugin.downloadChanged(PluginCoreUtils.wrap(dms[0]));
                        }
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                    }
                }
            }, true);
        }

        protected List getURLList(TOTorrent torrent, String key) {
            Object obj = torrent.getAdditionalProperty(key);
            if (obj instanceof byte[]) {
                List l = new ArrayList();
                l.add(obj);
                return (l);
            } else if (obj instanceof List) {
                return ((List) obj);
            } else {
                return (new ArrayList());
            }
        }
    });
    itemEditWebSeeds.setEnabled(dms.length == 1);
    // manual update
    final MenuItem itemManualUpdate = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemManualUpdate, // $NON-NLS-1$
    "GeneralView.label.trackerurlupdate");
    // itemManualUpdate.setImage(ImageRepository.getImage("edit_trackers"));
    itemManualUpdate.addListener(SWT.Selection, new ListenerDMTask(dms) {

        @Override
        public void run(DownloadManager dm) {
            dm.requestTrackerAnnounce(false);
        }
    });
    itemManualUpdate.setEnabled(manualUpdate);
    boolean scrape_enabled = COConfigurationManager.getBooleanParameter("Tracker Client Scrape Enable");
    boolean scrape_stopped = COConfigurationManager.getBooleanParameter("Tracker Client Scrape Stopped Enable");
    boolean manualScrape = (!scrape_enabled) || ((!scrape_stopped) && allStopped);
    final MenuItem itemManualScrape = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemManualScrape, "GeneralView.label.trackerscrapeupdate");
    // itemManualUpdate.setImage(ImageRepository.getImage("edit_trackers"));
    itemManualScrape.addListener(SWT.Selection, new ListenerDMTask(dms, true, true) {

        @Override
        public void run(DownloadManager dm) {
            dm.requestTrackerScrape(true);
        }
    });
    itemManualScrape.setEnabled(manualScrape);
    // download link
    final MenuItem itemTorrentDL = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemTorrentDL, "MyTorrentsView.menu.torrent.dl");
    itemTorrentDL.addListener(SWT.Selection, new ListenerDMTask(dms, false) {

        @Override
        public void run(DownloadManager dm) {
            String content;
            TOTorrent torrent = dm.getTorrent();
            String link = null;
            if (torrent == null) {
                content = "Torrent not available";
            } else {
                link = TorrentUtils.getObtainedFrom(torrent);
                if (link != null) {
                    try {
                        new URL(link);
                    } catch (Throwable e) {
                        link = null;
                    }
                }
                if (link != null) {
                    if (link.toLowerCase().startsWith("magnet:")) {
                        link = UrlUtils.getMagnetURI(dm);
                        content = "Torrent's magnet link:\r\n\r\n\t" + link;
                    } else {
                        content = "Torrent was obtained from\r\n\r\n\t" + link;
                    }
                } else {
                    if (TorrentUtils.isReallyPrivate(torrent)) {
                        content = "Origin of torrent unknown and it is private so a magnet URI can't be used - sorry!";
                    } else {
                        link = UrlUtils.getMagnetURI(dm);
                        content = "Origin unavailable but magnet URI may work:\r\n\r\n\t" + link;
                    }
                }
            }
            if (link != null) {
                ClipboardCopy.copyToClipBoard(link);
                content += "\r\n\r\nLink copied to clipboard";
            }
            final TextViewerWindow viewer = new TextViewerWindow(MessageText.getString("MyTorrentsView.menu.torrent.dl") + ": " + dm.getDisplayName(), null, content, false);
        }
    });
    itemTorrentDL.setEnabled(dms.length == 1);
    // move torrent
    final MenuItem itemFileMoveTorrent = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemFileMoveTorrent, "MyTorrentsView.menu.movetorrent");
    itemFileMoveTorrent.addListener(SWT.Selection, new ListenerDMTask(dms) {

        @Override
        public void run(DownloadManager[] dms) {
            TorrentUtil.moveTorrentFile(shell, dms);
        }
    });
    itemFileMoveTorrent.setEnabled(canMove);
    // switch torrent
    final MenuItem itemTorrentSwitch = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemTorrentSwitch, "MyTorrentsView.menu.torrent.switch");
    itemTorrentSwitch.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            final TOTorrent torrent = dms[0].getTorrent();
            if (torrent == null) {
                return;
            }
            try {
                byte[] existing_hash = torrent.getHash();
                FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
                dialog.setText(MessageText.getString("dialog.select.torrent.file"));
                dialog.setFilterExtensions(new String[] { "*.torrent" });
                dialog.setFilterNames(new String[] { "*.torrent" });
                String path = dialog.open();
                if (path == null) {
                    return;
                }
                File file = new File(path);
                byte[] replacement_hash = TOTorrentFactory.deserialiseFromBEncodedFile(file).getHash();
                if (!Arrays.equals(existing_hash, replacement_hash)) {
                    throw (new Exception("Hash mismatch: old=" + ByteFormatter.encodeString(existing_hash) + ", new=" + ByteFormatter.encodeString(replacement_hash)));
                }
                dms[0].setTorrentFileName(file.getAbsolutePath());
            } catch (Throwable e) {
                MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
                mb.setText(MessageText.getString("MyTorrentsView.menu.torrent.switch.fail"));
                mb.setMessage(MessageText.getString("MyTorrentsView.menu.torrent.switch.fail.text", new String[] { Debug.getNestedExceptionMessage(e) }));
                mb.open();
            }
        }
    });
    itemTorrentSwitch.setEnabled(dms.length == 1 && dms[0].isPersistent());
    // set source
    final MenuItem itemTorrentSource = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemTorrentSource, "MyTorrentsView.menu.torrent.set.source");
    itemTorrentSource.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            final TOTorrent torrent = dms[0].getTorrent();
            if (torrent == null) {
                return;
            }
            String msg_key_prefix = "MyTorrentsView.menu.edit_source.";
            SimpleTextEntryWindow text_entry = new SimpleTextEntryWindow();
            text_entry.setParentShell(shell);
            text_entry.setTitle(msg_key_prefix + "title");
            text_entry.setMessage(msg_key_prefix + "message");
            text_entry.setPreenteredText(TorrentUtils.getObtainedFrom(torrent), false);
            text_entry.setWidthHint(500);
            text_entry.prompt(new UIInputReceiverListener() {

                @Override
                public void UIInputReceiverClosed(UIInputReceiver text_entry) {
                    if (text_entry.hasSubmittedInput()) {
                        TorrentUtils.setObtainedFrom(torrent, text_entry.getSubmittedInput());
                        try {
                            TorrentUtils.writeToFile(torrent);
                        } catch (Throwable e) {
                        }
                    }
                }
            });
        }
    });
    itemTorrentSource.setEnabled(dms.length == 1);
    // set thumbnail
    final MenuItem itemTorrentThumb = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemTorrentThumb, "MyTorrentsView.menu.torrent.set.thumb");
    itemTorrentThumb.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            FileDialog fDialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
            fDialog.setText(MessageText.getString("MainWindow.dialog.choose.thumb"));
            String path = fDialog.open();
            if (path == null)
                return;
            File file = new File(path);
            try {
                byte[] thumbnail = FileUtil.readFileAsByteArray(file);
                String name = file.getName();
                int pos = name.lastIndexOf(".");
                String ext;
                if (pos != -1) {
                    ext = name.substring(pos + 1);
                } else {
                    ext = "";
                }
                String type = HTTPUtils.guessContentTypeFromFileType(ext);
                for (DownloadManager dm : dms) {
                    try {
                        TOTorrent torrent = dm.getTorrent();
                        PlatformTorrentUtils.setContentThumbnail(torrent, thumbnail, type);
                    } catch (Throwable e) {
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
    });
    itemTorrentThumb.setEnabled(hasSelection);
    // explore torrent file
    final MenuItem itemTorrentExplore = new MenuItem(menuTracker, SWT.PUSH);
    Messages.setLanguageText(itemTorrentExplore, "MyTorrentsView.menu." + (use_open_containing_folder ? "open_parent_folder" : "explore"));
    itemTorrentExplore.addListener(SWT.Selection, new ListenerDMTask(dms, false) {

        @Override
        public void run(DownloadManager dm) {
            ManagerUtils.open(new File(dm.getTorrentFileName()), use_open_containing_folder);
        }
    });
}
Also used : DiskManagerFileInfoSet(com.biglybt.core.disk.DiskManagerFileInfoSet) UIManagerListener(com.biglybt.pif.ui.UIManagerListener) UserPrompterResultListener(com.biglybt.ui.UserPrompterResultListener) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) WebSeedsEditorListener(com.biglybt.ui.swt.maketorrent.WebSeedsEditorListener) UIToolBarActivationListener(com.biglybt.pif.ui.toolbar.UIToolBarActivationListener) TrackerEditorListener(com.biglybt.ui.swt.maketorrent.TrackerEditorListener) DownloadManager(com.biglybt.core.download.DownloadManager) URL(java.net.URL) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) WebSeedsEditorListener(com.biglybt.ui.swt.maketorrent.WebSeedsEditorListener) List(java.util.List) WebSeedsEditor(com.biglybt.ui.swt.maketorrent.WebSeedsEditor) TrackerEditorListener(com.biglybt.ui.swt.maketorrent.TrackerEditorListener) PluginInterface(com.biglybt.pif.PluginInterface) MultiTrackerEditor(com.biglybt.ui.swt.maketorrent.MultiTrackerEditor) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) IOException(java.io.IOException) TOTorrent(com.biglybt.core.torrent.TOTorrent) ExternalSeedPlugin(com.biglybt.plugin.extseed.ExternalSeedPlugin) UIInputReceiver(com.biglybt.pif.ui.UIInputReceiver) MenuEvent(org.eclipse.swt.events.MenuEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) File(java.io.File)

Example 54 with TOTorrent

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

the class TorrentUtil method exportTorrent.

protected static void exportTorrent(DownloadManager[] dms, Shell parentShell) {
    // FileDialog for single download
    // DirectoryDialog for multiple.
    File[] destinations = new File[dms.length];
    if (dms.length == 1) {
        FileDialog fd = new FileDialog(parentShell, SWT.SAVE);
        fd.setFileName(dms[0].getTorrentFileName());
        String path = fd.open();
        if (path == null) {
            return;
        }
        destinations[0] = new File(path);
    } else {
        DirectoryDialog dd = new DirectoryDialog(parentShell, SWT.SAVE);
        String path = dd.open();
        if (path == null) {
            return;
        }
        for (int i = 0; i < dms.length; i++) {
            destinations[i] = new File(path, new File(dms[i].getTorrentFileName()).getName());
        }
    }
    int i = 0;
    try {
        for (; i < dms.length; i++) {
            File target = destinations[i];
            if (target.exists()) {
                MessageBox mb = new MessageBox(parentShell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
                mb.setText(MessageText.getString("exportTorrentWizard.process.outputfileexists.title"));
                mb.setMessage(MessageText.getString("exportTorrentWizard.process.outputfileexists.message") + "\n" + destinations[i].getName());
                int result = mb.open();
                if (result == SWT.NO) {
                    return;
                }
                if (!target.delete()) {
                    throw (new Exception("Failed to delete file"));
                }
            }
            // end deal with clashing torrent
            // first copy the torrent - DON'T use "writeTorrent" as this amends the
            // "filename" field in the torrent
            TorrentUtils.copyToFile(dms[i].getDownloadState().getTorrent(), target);
            // now remove the non-standard entries
            TOTorrent dest = TOTorrentFactory.deserialiseFromBEncodedFile(target);
            dest.removeAdditionalProperties();
            dest.serialiseToBEncodedFile(target);
        }
    // end for
    }// end try
     catch (Throwable e) {
        Logger.log(new LogAlert(dms[i], LogAlert.UNREPEATABLE, "Torrent export failed", e));
    }
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) File(java.io.File) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) IOException(java.io.IOException) LogAlert(com.biglybt.core.logging.LogAlert)

Example 55 with TOTorrent

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

the class TorrentUtil method addSpeedLimitsMenu.

protected static void addSpeedLimitsMenu(DownloadManager[] dms, Menu menu) {
    Core core = CoreFactory.getSingleton();
    Shell menu_shell = menu.getShell();
    final SpeedLimitHandler slh = SpeedLimitHandler.getSingleton(core);
    boolean all_have_limit = true;
    Set<String> common_profiles = new HashSet<>();
    final List<byte[]> dm_hashes = new ArrayList<>();
    for (int i = 0; i < dms.length; i++) {
        DownloadManager dm = dms[i];
        int maxul = dm.getStats().getUploadRateLimitBytesPerSecond();
        int maxdl = dm.getStats().getDownloadRateLimitBytesPerSecond();
        if (maxul == 0 && maxdl == 0) {
            all_have_limit = false;
        }
        TOTorrent t = dm.getTorrent();
        if (t == null) {
            common_profiles.clear();
        } else {
            try {
                byte[] hash = t.getHash();
                dm_hashes.add(hash);
                List<String> profs = slh.getProfilesForDownload(hash);
                if (i == 0) {
                    common_profiles.addAll(profs);
                } else {
                    common_profiles.retainAll(profs);
                }
            } catch (TOTorrentException e) {
                Debug.out(e);
                common_profiles.clear();
            }
        }
    }
    java.util.List<String> profiles = slh.getProfileNames();
    // add to profile
    final Menu add_to_prof_menu = new Menu(menu_shell, SWT.DROP_DOWN);
    MenuItem add_to_prof_item = new MenuItem(menu, SWT.CASCADE);
    add_to_prof_item.setMenu(add_to_prof_menu);
    Messages.setLanguageText(add_to_prof_item, "MyTorrentsView.menu.sl_add_to_prof");
    if (!all_have_limit) {
        add_to_prof_item.setEnabled(false);
    } else {
        for (final String p : profiles) {
            MenuItem addItem = new MenuItem(add_to_prof_menu, SWT.PUSH);
            addItem.setText(p);
            addItem.addListener(SWT.Selection, new Listener() {

                @Override
                public void handleEvent(Event arg0) {
                    slh.addDownloadsToProfile(p, dm_hashes);
                    MenuFactory.showText("MainWindow.menu.speed_limits.info.title", MessageText.getString("MainWindow.menu.speed_limits.info.prof", new String[] { p }), slh.getProfile(p));
                }
            });
        }
    }
    // remove from profile
    final Menu remove_from_prof_menu = new Menu(menu_shell, SWT.DROP_DOWN);
    MenuItem remove_from_prof_item = new MenuItem(menu, SWT.CASCADE);
    remove_from_prof_item.setMenu(remove_from_prof_menu);
    Messages.setLanguageText(remove_from_prof_item, "MyTorrentsView.menu.sl_remove_from_prof");
    if (common_profiles.isEmpty()) {
        remove_from_prof_item.setEnabled(false);
    } else {
        for (final String p : common_profiles) {
            MenuItem addItem = new MenuItem(remove_from_prof_menu, SWT.PUSH);
            addItem.setText(p);
            addItem.addListener(SWT.Selection, new Listener() {

                @Override
                public void handleEvent(Event arg0) {
                    slh.removeDownloadsFromProfile(p, dm_hashes);
                    MenuFactory.showText("MainWindow.menu.speed_limits.info.title", MessageText.getString("MainWindow.menu.speed_limits.info.prof", new String[] { p }), slh.getProfile(p));
                }
            });
        }
    }
}
Also used : UIManagerListener(com.biglybt.pif.ui.UIManagerListener) UserPrompterResultListener(com.biglybt.ui.UserPrompterResultListener) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) WebSeedsEditorListener(com.biglybt.ui.swt.maketorrent.WebSeedsEditorListener) UIToolBarActivationListener(com.biglybt.pif.ui.toolbar.UIToolBarActivationListener) TrackerEditorListener(com.biglybt.ui.swt.maketorrent.TrackerEditorListener) SpeedLimitHandler(com.biglybt.core.speedmanager.SpeedLimitHandler) DownloadManager(com.biglybt.core.download.DownloadManager) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) java.util(java.util) com.biglybt.core.util(com.biglybt.core.util) TOTorrent(com.biglybt.core.torrent.TOTorrent) MenuEvent(org.eclipse.swt.events.MenuEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Core(com.biglybt.core.Core) TableColumnCore(com.biglybt.ui.common.table.TableColumnCore)

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