📄 opentorrentwindow.java
字号:
// Use torrent in wherever it is and hope for the best
// XXX Should error instead?
torrentFile = new File(sFileName);
}
// Load up the torrent, see it it's real
try {
torrent = TorrentUtils.readFromFile(torrentFile, false);
} catch (final TOTorrentException e) {
Utils.execSWTThread(new AERunnable() {
public void runSupport() {
if (shell == null)
new MessagePopupShell(MessagePopupShell.ICON_ERROR,
"OpenTorrentWindow.mb.openError", Debug.getStackTrace(e),
new String[] { sOriginatingLocation, e.getMessage() },
MainWindow.getWindow().getDisplay());
else
Utils.openMessageBox(shell, SWT.OK,
"OpenTorrentWindow.mb.openError", new String[] {
sOriginatingLocation, e.getMessage() });
}
});
if (bDeleteFileOnCancel)
torrentFile.delete();
return null;
}
// Check if torrent already exists in gm, and add if not
final DownloadManager existingDownload = gm.getDownloadManager(torrent);
if (existingDownload == null) {
info = new TorrentInfo(torrentFile.getAbsolutePath(), torrent,
bDeleteFileOnCancel);
info.sOriginatingLocation = sOriginatingLocation;
torrentList.add(info);
} else {
Utils.execSWTThread(new AERunnable() {
public void runSupport() {
if (shell == null)
new MessagePopupShell(MessagePopupShell.ICON_ERROR,
"OpenTorrentWindow.mb.alreadyExists", null, new String[] {
sOriginatingLocation, existingDownload.getDisplayName() },
MainWindow.getWindow().getDisplay());
else
Utils.openMessageBox(shell, SWT.OK,
"OpenTorrentWindow.mb.alreadyExists", new String[] {
sOriginatingLocation, existingDownload.getDisplayName() });
}
});
if (bDeleteFileOnCancel)
torrentFile.delete();
}
return info;
}
private boolean isURL(String sURL) {
String sLower = sURL.toLowerCase();
return sLower.startsWith("http://") || sLower.startsWith("https://")
|| sLower.startsWith("magnet:") || sLower.startsWith("ftp://")
|| sLower.matches("^[a-fA-F0-9]{40}$");
}
private void updateOKButton() {
if (ok == null || bClosed)
return;
File file = new File(cmbDataDir.getText());
boolean bEnable = file.isDirectory() && torrentList != null
&& torrentList.size() > 0 && downloaders.size() == 0;
// Check for seeding
for (int i = 0; i < torrentList.size(); i++) {
boolean bTorrentValid = true;
TorrentInfo info = (TorrentInfo) torrentList.get(i);
if (info.iStartID == STARTMODE_SEEDING) {
// check if all selected files exist
TorrentFileInfo[] files = info.getFiles();
for (int j = 0; j < files.length; j++) {
TorrentFileInfo fileInfo = files[j];
if (!fileInfo.bDownload)
continue;
String sFullPath;
if (fileInfo.sDestFileName == null) {
File f = new File(info.sDestDir, fileInfo.sFullFileName);
sFullPath = f.getAbsolutePath();
} else {
sFullPath = fileInfo.sDestFileName;
}
file = new File(sFullPath);
if (!file.exists()) {
bEnable = false;
fileInfo.isValid = false;
bTorrentValid = false;
} else if (!fileInfo.isValid) {
fileInfo.isValid = true;
}
}
}
info.isValid = bTorrentValid;
}
ok.setEnabled(bEnable);
tableTorrents.clearAll();
dataFileTable.clearAll();
}
/**
* Resize the columns of the tables to fit without horizontal scrollbar
*
* @param which bitwise field of which table to recalc
* Bit 0: torrents table
* Bit 1: Data Files Table
*/
private void resizeTables(int which) {
try {
TableColumn[] tcs;
if ((which & 1) > 0) {
tcs = tableTorrents.getColumns();
int newSize = tableTorrents.getClientArea().width - 20;
int iLength = tcs.length;
if (Utils.LAST_TABLECOLUMN_EXPANDS) {
iLength--;
newSize -= ((Long) tcs[iLength].getData("Width")).intValue();
}
final int columnToExpand = 1;
for (int i = 0; i < iLength; i++)
if (i != columnToExpand)
newSize -= tcs[i].getWidth();
if (newSize > 10)
tcs[columnToExpand].setWidth(newSize);
}
// Adjust only first column
if ((which & 2) > 0) {
tcs = dataFileTable.getColumns();
int newSize = dataFileTable.getClientArea().width - 20;
int iLength = tcs.length;
if (Utils.LAST_TABLECOLUMN_EXPANDS) {
iLength--;
newSize -= ((Long) tcs[iLength].getData("Width")).intValue();
}
final int columnToExpand = 0;
for (int i = 0; i < iLength; i++)
if (i != columnToExpand)
newSize -= tcs[i].getWidth();
if (newSize > 10)
tcs[columnToExpand].setWidth(newSize);
}
} catch (Exception e) {
// ignore
e.printStackTrace();
}
}
/**
* Open the torrents already added based on user choices
*
* @param sDataDir
*/
private void openTorrents() {
ArrayList addedTorrentsTop = new ArrayList();
for (int i = 0; i < torrentList.size(); i++) {
TorrentInfo info = (TorrentInfo) torrentList.get(i);
try {
if (info.torrent == null)
continue;
// set "queued" to STATE_WAITING so that auto-open details will work
// (even if the torrent immediately goes to queued)
int iStartMode = (info.iStartID == STARTMODE_STOPPED)
? DownloadManager.STATE_STOPPED : DownloadManager.STATE_WAITING;
DownloadManager dm = gm
.addDownloadManager(info.sFileName, info.sDestDir, iStartMode,
true, info.iStartID == STARTMODE_SEEDING);
// If dm is null, most likely there was an error printed.. let's hope
// the user was notified and skip the error quietly.
// We don't have to worry about deleting the file (info.bDelete..)
// since gm.addDown.. will handle it.
if (dm == null)
continue;
if (info.iQueueLocation == QUEUELOCATION_TOP)
addedTorrentsTop.add(dm);
if (iStartMode == STARTMODE_FORCESTARTED)
dm.setForceStart(true);
TorrentFileInfo[] files = info.getFiles();
DiskManagerFileInfo[] dmFileInfo = dm.getDiskManagerFileInfo();
for (int j = 0; j < dmFileInfo.length; j++) {
int iIndex = dmFileInfo[j].getIndex();
if (iIndex >= 0 && iIndex < files.length
&& files[iIndex].lSize == dmFileInfo[j].getLength()) {
File fDest;
if (files[iIndex].sDestFileName != null) {
fDest = new File(files[iIndex].sDestFileName);
dmFileInfo[j].setLink(fDest);
} else {
fDest = new File(info.sDestDir, files[iIndex].sFullFileName);
}
if (!files[iIndex].bDownload) {
dmFileInfo[j].setSkipped(true);
if (!fDest.exists()) {
dmFileInfo[j].setStorageType(DiskManagerFileInfo.ST_COMPACT);
}
}
}
}
} catch (Exception e) {
if (shell == null)
new MessagePopupShell(MessagePopupShell.ICON_ERROR,
"OpenTorrentWindow.mb.openError", Debug.getStackTrace(e),
new String[] { info.sOriginatingLocation, e.getMessage() },
MainWindow.getWindow().getDisplay());
else
Utils.openMessageBox(shell, SWT.OK, "OpenTorrentWindow.mb.openError",
new String[] { info.sOriginatingLocation, e.getMessage() });
}
}
if (addedTorrentsTop.size() > 0) {
DownloadManager[] dms = (DownloadManager[]) addedTorrentsTop
.toArray(new DownloadManager[0]);
gm.moveTop(dms);
}
torrentList.clear();
}
private int getDefaultStartMode() {
if (bDefaultForSeeding)
return STARTMODE_SEEDING;
return (bOverrideStartModeToStopped || COConfigurationManager
.getBooleanParameter("Default Start Torrents Stopped"))
? STARTMODE_STOPPED : STARTMODE_QUEUED;
}
// TorrentDownloaderCallBackInterface
public void TorrentDownloaderEvent(int state, final TorrentDownloader inf) {
// This method is run even if the window is closed.
if (state == TorrentDownloader.STATE_INIT) {
downloaders.add(inf);
} else if (state == TorrentDownloader.STATE_FINISHED) {
// This can be called more than once for each inf..
if (!downloaders.contains(inf))
return;
downloaders.remove(inf);
File file = inf.getFile();
if (addTorrent(file.getAbsolutePath(), inf.getURL()) == null) {
// addTorrent may not delete it on error if the downloader saved it
// to the place where user wants to store torrents (which is most
// likely)
if (file.exists())
file.delete();
return;
}
if (shell != null && !shell.isDisposed()) {
Utils.execSWTThread(new AERunnable() {
public void runSupport() {
tableTorrents.setItemCount(torrentList.size());
tableTorrents.clearAll();
// select the one we just added
tableTorrents.select(torrentList.size() - 1);
// select doesn't notify listeners? Do it manually.
tableTorrents.notifyListeners(SWT.Selection, new Event());
resizeTables(1);
}
});
} else {
String saveSilentlyDir = getSaveSilentlyDir();
if (saveSilentlyDir != null) {
sDestDir = saveSilentlyDir;
openTorrents();
}
}
} else if (state == TorrentDownloader.STATE_CANCELLED
|| state == TorrentDownloader.STATE_ERROR
|| state == TorrentDownloader.STATE_DUPLICATE) {
downloaders.remove(inf);
} else
return;
// definitely on a different thread..
Utils.execSWTThread(new AERunnable() {
public void runSupport() {
updateOKButton();
}
});
}
/**
* Class to store one Torrent file's info. Used to populate table and store
* user's choices.
*/
private class TorrentInfo {
/** Where the torrent came from. Could be a file, URL, or some other text */
String sOriginatingLocation;
/** Filename the .torrent is saved to */
String sFileName;
String sDestDir;
TOTorrent torrent;
int iStartID;
int iQueueLocation;
boolean isValid;
boolean bDeleteFileOnCancel;
private TorrentFileInfo[] files = null;
/**
* Init
*
* @param sFileName
* @param torrent
* @param bDeleteFileOnCancel
*/
public TorrentInfo(String sFileName, TOTorrent torrent,
boolean bDeleteFileOnCancel) {
this.bDeleteFileOnCancel = bDeleteFileOnCancel;
this.sFileName = sFileName;
this.sOriginatingLocation = sFileName;
this.torrent = torrent;
this.sDestDir = OpenTorrentWindow.this.sDestDir;
iStartID = getDefaultStartMode();
iQueueLocation = QUEUELOCATION_BOTTOM;
isValid = true;
// Force a check on the encoding, will prompt user if we dunno
try {
LocaleUtil.getSingleton().getTorrentEncoding(TorrentInfo.this.torrent);
} catch (Exception e) {
e.printStackTrace();
}
}
public TorrentFileInfo[] getFiles() {
if (files == null && torrent != null) {
TOTorrentFile[] tfiles = torrent.getFiles();
files = new TorrentFileInfo[tfiles.length];
for (int i = 0; i < files.length; i++) {
files[i] = new TorrentFileInfo(this, tfiles[i], i);
}
}
return files;
}
public String getTorrentName() {
if (torrent == null)
return "";
try {
LocaleUtilDecoder decoder = LocaleUtil.getSingleton()
.getTorrentEncodingIfAvailable(torrent);
if (decoder != null)
return decoder.decodeString(torrent.getName());
} catch (Exception e) {
}
try {
return new String(torrent.getName());
} catch (Exception e) {
return "TextDecodingError";
}
}
public boolean allFilesMoving() {
TorrentFileInfo[] files = getFiles();
for (int j = 0; j < files.length; j++) {
if (files[j].sDestFileName == null) {
return false;
}
}
return true;
}
}
/**
* Class to store the file list of a Torrent. Used to populate table and
* store user's choices
*/
private class TorrentFileInfo {
String sFileName;
String sFullFileName;
long lSize;
boolean bDownload;
String sDestFileName;
long iIndex;
boolean isValid;
final TorrentInfo parent;
/**
* Init
*
* @param parent
* @param torrentFile
* @param iIndex
*/
public TorrentFileInfo(TorrentInfo parent, TOTorrentFile torrentFile,
int iIndex) {
this.parent = parent;
lSize = torrentFile.getLength();
this.iIndex = iIndex;
bDownload = true;
sDestFileName = null;
isValid = true;
sFileName = torrentFile.getRelativePath(); // translated to locale
if (parent.torrent.isSimpleTorrent()) {
sFullFileName = sFileName;
} else {
sFullFileName = parent.getTorrentName() + File.separator
+ torrentFile.getRelativePath();
}
}
public String getDestPath() {
if (sDestFileName != null)
return new File(sDestFileName).getParent();
if (parent.torrent.isSimpleTorrent())
return parent.sDestDir;
return new File(parent.sDestDir, sFullFileName).getParent();
}
public boolean okToDisable() {
return lSize >= MIN_NODOWNLOAD_SIZE
|| parent.iStartID == STARTMODE_SEEDING;
}
}
private String ensureTrailingSeparator(String sPath) {
if (sPath == null || sPath.length() == 0 || sPath.endsWith(File.separator))
return sPath;
return sPath + File.separator;
}
/**
*
* @return Null if user doesn't want to save silently, or if no path set
*/
private static String getSaveSilentlyDir() {
boolean bUseDefault = COConfigurationManager
.getBooleanParameter("Use default data dir");
if (!bUseDefault)
return null;
String sDefDir = "";
try {
sDefDir = COConfigurationManager.getDirectoryParameter(PARAM_DEFSAVEPATH);
} catch (IOException e) {
return null;
}
return (sDefDir == "") ? null : sDefDir;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -