Search in sources :

Example 1 with WebSeedsEditorListener

use of com.biglybt.ui.swt.maketorrent.WebSeedsEditorListener 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)

Aggregations

DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 PluginInterface (com.biglybt.pif.PluginInterface)1 UIInputReceiver (com.biglybt.pif.ui.UIInputReceiver)1 UIInputReceiverListener (com.biglybt.pif.ui.UIInputReceiverListener)1 UIManagerListener (com.biglybt.pif.ui.UIManagerListener)1 UIToolBarActivationListener (com.biglybt.pif.ui.toolbar.UIToolBarActivationListener)1 ExternalSeedPlugin (com.biglybt.plugin.extseed.ExternalSeedPlugin)1 UserPrompterResultListener (com.biglybt.ui.UserPrompterResultListener)1 MultiTrackerEditor (com.biglybt.ui.swt.maketorrent.MultiTrackerEditor)1 TrackerEditorListener (com.biglybt.ui.swt.maketorrent.TrackerEditorListener)1 WebSeedsEditor (com.biglybt.ui.swt.maketorrent.WebSeedsEditor)1 WebSeedsEditorListener (com.biglybt.ui.swt.maketorrent.WebSeedsEditorListener)1 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 List (java.util.List)1