{
Map<String,List<DownloadManager>> same_map = new HashMap<String, List<DownloadManager>>();
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<DownloadManager>();
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,
group,
new TrackerEditorListener()
{
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);
}
}
});
itemEditTracker.setEnabled(hasSelection);
// edit webseeds
final MenuItem itemEditWebSeeds = new MenuItem(menuTracker, SWT.PUSH);
Messages.setLanguageText(itemEditWebSeeds, "MyTorrentsView.menu.editWebSeeds");
itemEditWebSeeds.addListener(SWT.Selection, new DMTask(dms) {
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()
{
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 = AzureusCoreFactory.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, "GeneralView.label.trackerurlupdate"); //$NON-NLS-1$
//itemManualUpdate.setImage(ImageRepository.getImage("edit_trackers"));
itemManualUpdate.addListener(SWT.Selection, new DMTask(dms) {
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 DMTask(dms, true, true ) {
public void run(DownloadManager dm) {
dm.requestTrackerScrape(true);
}
});
itemManualScrape.setEnabled(manualScrape);
// advanced > files
final MenuItem itemFiles = new MenuItem(menuAdvanced, SWT.CASCADE);
Messages.setLanguageText(itemFiles, "ConfigView.section.files");
final Menu menuFiles = new Menu(composite.getShell(), SWT.DROP_DOWN);
itemFiles.setMenu(menuFiles);
final MenuItem itemFileMoveData = new MenuItem(menuFiles, SWT.PUSH);
Messages.setLanguageText(itemFileMoveData, "MyTorrentsView.menu.movedata");
itemFileMoveData.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager[] dms) {
if (dms != null && dms.length > 0) {
DirectoryDialog dd = new DirectoryDialog(composite.getShell());
dd.setFilterPath(TorrentOpener.getFilterPathData());
dd.setText(MessageText.getString("MyTorrentsView.menu.movedata.dialog"));
String path = dd.open();
if (path != null) {
TorrentOpener.setFilterPathData(path);
File target = new File(path);
for (int i = 0; i < dms.length; i++) {
try {
dms[i].moveDataFiles(target);
}
catch (Throwable e) {
Logger.log(new LogAlert(dms[i], LogAlert.REPEATABLE,
"Download data move operation failed", e));
}
}
}
}
}
});
itemFileMoveData.setEnabled(fileMove);
final MenuItem itemFileMoveTorrent = new MenuItem(menuFiles, SWT.PUSH);
Messages.setLanguageText(itemFileMoveTorrent, "MyTorrentsView.menu.movetorrent");
itemFileMoveTorrent.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager[] dms) {
if (dms != null && dms.length > 0) {
DirectoryDialog dd = new DirectoryDialog(composite.getShell());
String filter_path = TorrentOpener.getFilterPathTorrent();
// If we don't have a decent path, default to the path of the first
// torrent.
if (filter_path == null || filter_path.trim().length() == 0) {
filter_path = new File(dms[0].getTorrentFileName()).getParent();
}
dd.setFilterPath(filter_path);
dd.setText(MessageText.getString("MyTorrentsView.menu.movedata.dialog"));
String path = dd.open();
if (path != null) {
File target = new File(path);
TorrentOpener.setFilterPathTorrent(target.toString());
for (int i = 0; i < dms.length; i++) {
try {
dms[i].moveTorrentFile(target);
}
catch (Throwable e) {
Logger.log(new LogAlert(dms[i], LogAlert.REPEATABLE,
"Download torrent move operation failed", e));
}
}
}
}
}
});
itemFileMoveTorrent.setEnabled(fileMove);
final MenuItem itemCheckFilesExist = new MenuItem(menuFiles, SWT.PUSH);
Messages.setLanguageText(itemCheckFilesExist, "MyTorrentsView.menu.checkfilesexist");
itemCheckFilesExist.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager dm) {
dm.filesExist( true );
}
});
final MenuItem itemFileRescan = new MenuItem(menuFiles, SWT.CHECK);
Messages.setLanguageText(itemFileRescan, "MyTorrentsView.menu.rescanfile");
itemFileRescan.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager dm) {
dm.getDownloadState().setFlag(DownloadManagerState.FLAG_SCAN_INCOMPLETE_PIECES,
itemFileRescan.getSelection());
}
});
itemFileRescan.setSelection(allScanSelected);
itemFileRescan.setEnabled(fileRescan);
// clear allocation
MenuItem itemFileClearAlloc = new MenuItem(menuFiles, SWT.PUSH);
Messages.setLanguageText(itemFileClearAlloc, "MyTorrentsView.menu.clear_alloc_data");
itemFileClearAlloc.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager dm) {
dm.setDataAlreadyAllocated( false );
}
});
itemFileClearAlloc.setEnabled(allStopped);
// clear resume
MenuItem itemFileClearResume = new MenuItem(menuFiles, SWT.PUSH);
Messages.setLanguageText(itemFileClearResume, "MyTorrentsView.menu.clear_resume_data");
itemFileClearResume.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager dm) {
dm.getDownloadState().clearResumeData();
}
});
itemFileClearResume.setEnabled(allStopped);
// Advanced - > Rename
final MenuItem itemRename = new MenuItem(menuAdvanced, SWT.DROP_DOWN);
Messages.setLanguageText(itemRename, "MyTorrentsView.menu.rename");
itemRename.setEnabled(hasSelection);
itemRename.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
for (DownloadManager dm : dms) {
AdvRenameWindow window = new AdvRenameWindow();
window.open(dm);
}
}
});
// === advanced > export ===
// =========================
if (userMode > 0) {
final MenuItem itemExport = new MenuItem(menuAdvanced, SWT.CASCADE);
Messages.setLanguageText(itemExport, "MyTorrentsView.menu.exportmenu"); //$NON-NLS-1$
Utils.setMenuItemImage(itemExport, "export");
itemExport.setEnabled(hasSelection);
final Menu menuExport = new Menu(composite.getShell(), SWT.DROP_DOWN);
itemExport.setMenu(menuExport);
// Advanced > Export > Export XML
final MenuItem itemExportXML = new MenuItem(menuExport, SWT.PUSH);
Messages.setLanguageText(itemExportXML, "MyTorrentsView.menu.export");
itemExportXML.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager[] dms) {
DownloadManager dm = dms[0]; // First only.
if (dm != null) new ExportTorrentWizard(itemExportXML.getDisplay(), dm);
}
});
// Advanced > Export > Export Torrent
final MenuItem itemExportTorrent = new MenuItem(menuExport, SWT.PUSH);
Messages.setLanguageText(itemExportTorrent, "MyTorrentsView.menu.exporttorrent");
itemExportTorrent.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager[] dms) {
// FileDialog for single download
// DirectoryDialog for multiple.
File[] destinations = new File[dms.length];
if (dms.length == 1) {
FileDialog fd = new FileDialog(composite.getShell(), 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(composite.getShell(), 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(composite.getShell(), 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));
}
} // end run()
}); // end DMTask
// Advanced > Export > WebSeed URL
final MenuItem itemWebSeed = new MenuItem(menuExport, SWT.PUSH);
Messages.setLanguageText(itemWebSeed, "MyTorrentsView.menu.exporthttpseeds");
itemWebSeed.addListener(SWT.Selection, new DMTask(dms) {
public void run(DownloadManager[] dms) {
final String NL = "\r\n";
String data = "";
boolean http_enable = COConfigurationManager.getBooleanParameter( "HTTP.Data.Listen.Port.Enable" );
String port;
if ( http_enable ){
int p = COConfigurationManager.getIntParameter( "HTTP.Data.Listen.Port" );
int o = COConfigurationManager.getIntParameter( "HTTP.Data.Listen.Port.Override" );
if ( o == 0 ){
port = String.valueOf( p );
}else{
port = String.valueOf( o );
}
}else{
data = "You need to enable the HTTP port or modify the URL(s) appropriately" + NL + NL;
port = "<port>";
}
String ip = COConfigurationManager.getStringParameter( "Tracker IP", "" );
if ( ip.length() == 0 ){
data += "You might need to modify the host address in the URL(s)" + NL + NL;
try{
InetAddress ia = AzureusCoreFactory.getSingleton().getInstanceManager().getMyInstance().getExternalAddress();
if ( ia != null ){
ip = IPToHostNameResolver.syncResolve( ia.getHostAddress(), 10000 );
}
}catch( Throwable e ){
}
if ( ip.length() == 0 ){
ip = "<host>";
}
}
String base = "http://" + UrlUtils.convertIPV6Host(ip) + ":" + port + "/";
for (int i=0;i<dms.length;i++){
DownloadManager dm = dms[i];
if ( dm == null ){
continue;
}
TOTorrent torrent = dm.getTorrent();
if ( torrent == null ){
continue;
}
data += base + "webseed" + NL;
try{
data += base + "files/" + URLEncoder.encode( new String( torrent.getHash(), "ISO-8859-1"), "ISO-8859-1" ) + "/" + NL + NL;
}catch( Throwable e ){
}
}