use of com.biglybt.plugin.extseed.ExternalSeedPlugin in project BiglyBT by BiglySoftware.
the class DownloadManagerImpl method getTrackerPeerSources.
@Override
public List<TrackerPeerSource> getTrackerPeerSources() {
try {
this_mon.enter();
Object[] tps_data = (Object[]) getUserData(TPS_Key);
List<TrackerPeerSource> tps;
if (tps_data == null) {
tps = new ArrayList<>();
TOTorrentListener tol = new TOTorrentListener() {
@Override
public void torrentChanged(TOTorrent torrent, int type) {
if (type == TOTorrentListener.CT_ANNOUNCE_URLS) {
List<DownloadManagerTPSListener> to_inform = null;
try {
this_mon.enter();
torrent.removeListener(this);
setUserData(TPS_Key, null);
if (tps_listeners != null) {
to_inform = new ArrayList<>(tps_listeners);
}
} finally {
this_mon.exit();
}
if (to_inform != null) {
for (DownloadManagerTPSListener l : to_inform) {
try {
l.trackerPeerSourcesChanged();
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
}
};
setUserData(TPS_Key, new Object[] { tps, tol });
Download plugin_download = PluginCoreUtils.wrap(this);
if (isDestroyed() || plugin_download == null) {
return (tps);
}
// tracker peer sources
final TOTorrent t = getTorrent();
if (t != null) {
t.addListener(tol);
TOTorrentAnnounceURLSet[] sets = t.getAnnounceURLGroup().getAnnounceURLSets();
if (sets.length == 0) {
sets = new TOTorrentAnnounceURLSet[] { t.getAnnounceURLGroup().createAnnounceURLSet(new URL[] { torrent.getAnnounceURL() }) };
}
for (final TOTorrentAnnounceURLSet set : sets) {
final URL[] urls = set.getAnnounceURLs();
if (urls.length == 0 || TorrentUtils.isDecentralised(urls[0])) {
continue;
}
tps.add(new TrackerPeerSource() {
private TrackerPeerSource _delegate;
private TRTrackerAnnouncer ta;
private long ta_fixup;
private long last_scrape_fixup_time;
private Object[] last_scrape;
private TrackerPeerSource fixup() {
long now = SystemTime.getMonotonousTime();
if (now - ta_fixup > 1000) {
TRTrackerAnnouncer current_ta = getTrackerClient();
if (current_ta == ta) {
if (current_ta != null && _delegate == null) {
_delegate = current_ta.getTrackerPeerSource(set);
}
} else {
if (current_ta == null) {
_delegate = null;
} else {
_delegate = current_ta.getTrackerPeerSource(set);
}
ta = current_ta;
}
ta_fixup = now;
}
return (_delegate);
}
protected Object[] getScrape() {
long now = SystemTime.getMonotonousTime();
if (now - last_scrape_fixup_time > 30 * 1000 || last_scrape == null) {
TRTrackerScraper scraper = globalManager.getTrackerScraper();
int max_peers = -1;
int max_seeds = -1;
int max_comp = -1;
int max_time = 0;
int min_scrape = Integer.MAX_VALUE;
String status_str = null;
boolean found_usable = false;
for (URL u : urls) {
TRTrackerScraperResponse resp = scraper.peekScrape(torrent, u);
if (resp != null) {
if (!resp.isDHTBackup()) {
found_usable = true;
int peers = resp.getPeers();
int seeds = resp.getSeeds();
int comp = resp.getCompleted();
if (peers > max_peers) {
max_peers = peers;
}
if (seeds > max_seeds) {
max_seeds = seeds;
}
if (comp > max_comp) {
max_comp = comp;
}
if (resp.getStatus() != TRTrackerScraperResponse.ST_INITIALIZING) {
status_str = resp.getStatusString();
int time = resp.getScrapeTime();
if (time > max_time) {
max_time = time;
}
long next_scrape = resp.getNextScrapeStartTime();
if (next_scrape > 0) {
int ns = (int) (next_scrape / 1000);
if (ns < min_scrape) {
min_scrape = ns;
}
}
}
}
}
}
if (found_usable || last_scrape == null) {
last_scrape = new Object[] { max_seeds, max_peers, max_time, min_scrape, max_comp, status_str };
}
last_scrape_fixup_time = now;
}
return (last_scrape);
}
@Override
public int getType() {
return (TrackerPeerSource.TP_TRACKER);
}
@Override
public String getName() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (urls[0].toExternalForm());
}
return (delegate.getName());
}
@Override
public int getStatus() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (ST_STOPPED);
}
return (delegate.getStatus());
}
@Override
public String getStatusString() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((String) getScrape()[5]);
}
return (delegate.getStatusString());
}
@Override
public int getSeedCount() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((Integer) getScrape()[0]);
}
int seeds = delegate.getSeedCount();
if (seeds < 0) {
seeds = (Integer) getScrape()[0];
}
return (seeds);
}
@Override
public int getLeecherCount() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((Integer) getScrape()[1]);
}
int leechers = delegate.getLeecherCount();
if (leechers < 0) {
leechers = (Integer) getScrape()[1];
}
return (leechers);
}
@Override
public int getCompletedCount() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((Integer) getScrape()[4]);
}
int comp = delegate.getCompletedCount();
if (comp < 0) {
comp = (Integer) getScrape()[4];
}
return (comp);
}
@Override
public int getPeers() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (-1);
}
return (delegate.getPeers());
}
@Override
public int getInterval() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
Object[] si = getScrape();
int last = (Integer) si[2];
int next = (Integer) si[3];
if (last > 0 && next < Integer.MAX_VALUE && last < next) {
return (next - last);
}
return (-1);
}
return (delegate.getInterval());
}
@Override
public int getMinInterval() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (-1);
}
return (delegate.getMinInterval());
}
@Override
public boolean isUpdating() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (false);
}
return (delegate.isUpdating());
}
@Override
public int getLastUpdate() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((Integer) getScrape()[2]);
}
return (delegate.getLastUpdate());
}
@Override
public int getSecondsToUpdate() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (-1);
}
return (delegate.getSecondsToUpdate());
}
@Override
public boolean canManuallyUpdate() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (false);
}
return (delegate.canManuallyUpdate());
}
@Override
public void manualUpdate() {
TrackerPeerSource delegate = fixup();
if (delegate != null) {
delegate.manualUpdate();
}
}
@Override
public boolean canDelete() {
return (true);
}
@Override
public void delete() {
List<List<String>> lists = TorrentUtils.announceGroupsToList(t);
List<String> rem = new ArrayList<>();
for (URL u : urls) {
rem.add(u.toExternalForm());
}
lists = TorrentUtils.removeAnnounceURLs2(lists, rem, false);
TorrentUtils.listToAnnounceGroups(lists, t);
}
});
}
// cache peer source
tps.add(new TrackerPeerSourceAdapter() {
private TrackerPeerSource _delegate;
private TRTrackerAnnouncer ta;
private boolean enabled;
private long ta_fixup;
private TrackerPeerSource fixup() {
long now = SystemTime.getMonotonousTime();
if (now - ta_fixup > 1000) {
TRTrackerAnnouncer current_ta = getTrackerClient();
if (current_ta == ta) {
if (current_ta != null && _delegate == null) {
_delegate = current_ta.getCacheTrackerPeerSource();
}
} else {
if (current_ta == null) {
_delegate = null;
} else {
_delegate = current_ta.getCacheTrackerPeerSource();
}
ta = current_ta;
}
enabled = controller.isPeerSourceEnabled(PEPeerSource.PS_BT_TRACKER);
ta_fixup = now;
}
return (_delegate);
}
@Override
public int getType() {
return (TrackerPeerSource.TP_TRACKER);
}
@Override
public String getName() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (MessageText.getString("tps.tracker.cache"));
}
return (delegate.getName());
}
@Override
public int getStatus() {
TrackerPeerSource delegate = fixup();
if (!enabled) {
return (ST_DISABLED);
}
if (delegate == null) {
return (ST_STOPPED);
}
return (ST_ONLINE);
}
@Override
public int getPeers() {
TrackerPeerSource delegate = fixup();
if (delegate == null || !enabled) {
return (-1);
}
return (delegate.getPeers());
}
});
}
try {
ExternalSeedPlugin esp = DownloadManagerController.getExternalSeedPlugin();
if (esp != null) {
tps.add(esp.getTrackerPeerSource(plugin_download));
}
} catch (Throwable e) {
}
try {
PluginInterface dht_pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass(DHTTrackerPlugin.class);
if (dht_pi != null) {
tps.add(((DHTTrackerPlugin) dht_pi.getPlugin()).getTrackerPeerSource(plugin_download));
}
} catch (Throwable e) {
}
try {
PluginInterface lt_pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass(LocalTrackerPlugin.class);
if (lt_pi != null) {
tps.add(((LocalTrackerPlugin) lt_pi.getPlugin()).getTrackerPeerSource(plugin_download));
}
} catch (Throwable e) {
}
try {
tps.add(((DownloadImpl) plugin_download).getTrackerPeerSource());
} catch (Throwable e) {
}
// PEX...
tps.add(new TrackerPeerSourceAdapter() {
private PEPeerManager _pm;
private TrackerPeerSource _delegate;
private TrackerPeerSource fixup() {
PEPeerManager pm = getPeerManager();
if (pm == null) {
_delegate = null;
_pm = null;
} else if (pm != _pm) {
_pm = pm;
_delegate = pm.getTrackerPeerSource();
}
return (_delegate);
}
@Override
public int getType() {
return (TP_PEX);
}
@Override
public int getStatus() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (ST_STOPPED);
} else {
return (delegate.getStatus());
}
}
@Override
public String getName() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ("");
} else {
return (delegate.getName());
}
}
@Override
public int getPeers() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (-1);
} else {
return (delegate.getPeers());
}
}
});
// incoming
tps.add(new TrackerPeerSourceAdapter() {
private long fixup_time;
private PEPeerManager _pm;
private int tcp;
private int udp;
private int utp;
private int total;
private boolean enabled;
private PEPeerManager fixup() {
long now = SystemTime.getMonotonousTime();
if (now - fixup_time > 1000) {
PEPeerManager pm = _pm = getPeerManager();
if (pm != null) {
tcp = pm.getNbRemoteTCPConnections();
udp = pm.getNbRemoteUDPConnections();
utp = pm.getNbRemoteUTPConnections();
total = pm.getStats().getTotalIncomingConnections();
}
enabled = controller.isPeerSourceEnabled(PEPeerSource.PS_INCOMING);
fixup_time = now;
}
return (_pm);
}
@Override
public int getType() {
return (TP_INCOMING);
}
@Override
public int getStatus() {
PEPeerManager delegate = fixup();
if (delegate == null) {
return (ST_STOPPED);
} else if (!enabled) {
return (ST_DISABLED);
} else {
return (ST_ONLINE);
}
}
@Override
public String getName() {
PEPeerManager delegate = fixup();
if (delegate == null || !enabled) {
return ("");
} else {
return (MessageText.getString("tps.incoming.details", new String[] { String.valueOf(tcp), String.valueOf(udp + utp), String.valueOf(total) }));
}
}
@Override
public int getPeers() {
PEPeerManager delegate = fixup();
if (delegate == null || !enabled) {
return (-1);
} else {
return (tcp + udp);
}
}
});
} else {
tps = (List<TrackerPeerSource>) tps_data[0];
}
return (tps);
} finally {
this_mon.exit();
}
}
use of com.biglybt.plugin.extseed.ExternalSeedPlugin 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);
}
});
}
use of com.biglybt.plugin.extseed.ExternalSeedPlugin in project BiglyBT by BiglySoftware.
the class DownloadManagerController method addHTTPSeed.
@Override
public void addHTTPSeed(String address, int port) {
ExternalSeedPlugin plugin = getExternalSeedPlugin();
try {
if (plugin != null) {
Map config = new HashMap();
List urls = new ArrayList();
String seed_url = "http://" + UrlUtils.convertIPV6Host(address) + ":" + port + "/webseed";
urls.add(seed_url.getBytes());
config.put("httpseeds", urls);
Map params = new HashMap();
params.put("supports_503", new Long(0));
params.put("transient", new Long(1));
config.put("httpseeds-params", params);
List<ExternalSeedPeer> new_seeds = plugin.addSeed(com.biglybt.pifimpl.local.download.DownloadManagerImpl.getDownloadStatic(download_manager), config);
if (new_seeds.size() > 0) {
List<ExternalSeedPeer> to_remove = new ArrayList<>();
synchronized (http_seeds) {
http_seeds.addAll(new_seeds);
while (http_seeds.size() > HTTP_SEEDS_MAX) {
ExternalSeedPeer x = http_seeds.removeFirst();
to_remove.add(x);
}
}
for (ExternalSeedPeer peer : to_remove) {
peer.remove();
}
}
}
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
Aggregations