📄 opentorrentwindow.java
字号:
/*
* OpenTorrentWindow.java
*
* Created on February 23, 2004, 4:09 PM
*
* Copyright (C) 2004, 2005, 2006 Aelitis SAS, All rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details ( see the LICENSE file ).
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* AELITIS, SAS au capital de 46,603.30 euros,
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*/
package org.gudy.azureus2.ui.swt;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.config.StringIterator;
import org.gudy.azureus2.core3.config.StringList;
import org.gudy.azureus2.core3.disk.DiskManagerFileInfo;
import org.gudy.azureus2.core3.download.DownloadManager;
import org.gudy.azureus2.core3.global.GlobalManager;
import org.gudy.azureus2.core3.internat.LocaleUtil;
import org.gudy.azureus2.core3.internat.LocaleUtilDecoder;
import org.gudy.azureus2.core3.internat.MessageText;
import org.gudy.azureus2.core3.torrent.TOTorrent;
import org.gudy.azureus2.core3.torrent.TOTorrentException;
import org.gudy.azureus2.core3.torrent.TOTorrentFile;
import org.gudy.azureus2.core3.torrentdownloader.TorrentDownloader;
import org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.ui.swt.mainwindow.Colors;
import org.gudy.azureus2.ui.swt.mainwindow.MainWindow;
import org.gudy.azureus2.ui.swt.mainwindow.TorrentOpener;
import org.gudy.azureus2.ui.swt.shells.MessagePopupShell;
/**
* Torrent Opener Window.
*
* @author TuxPaper
*
* @TODO: Category Option
*/
public class OpenTorrentWindow implements TorrentDownloaderCallBackInterface {
/** Don't allow disabling of downloading for files smaller than this */
private final static int MIN_NODOWNLOAD_SIZE = 1024 * 1024;
private final static String PARAM_DEFSAVEPATH = "Default save path";
private final static int STARTMODE_QUEUED = 0;
private final static int STARTMODE_STOPPED = 1;
private final static int STARTMODE_FORCESTARTED = 2;
private final static int STARTMODE_SEEDING = 3;
private final static int QUEUELOCATION_TOP = 0;
private final static int QUEUELOCATION_BOTTOM = 1;
private final static String[] startModes = { "queued", "stopped",
"forceStarted", "seeding" };
private final static String[] queueLocations = { "first", "last" };
/** Only one window, since it can handle multiple torrents */
private static OpenTorrentWindow stOpenTorrentWindow = null;
// SWT Stuff
private Shell shell;
private Table dataFileTable;
private Table tableTorrents;
private Button ok;
private Combo cmbDataDir;
private Combo cmbStartMode = null;
private Combo cmbQueueLocation = null;
// Link to the outside
private GlobalManager gm;
// Internal Stuff
/** TorrentFileInfo list. All dataFiles currently in table, same order */
private ArrayList dataFiles = new ArrayList();
/** TorrentInfo list. All torrents to open, same order as table */
private ArrayList torrentList = new ArrayList();
/** List of torrents being downloaded. Stored so we don't close window
* until they are done/aborted.
*/
private ArrayList downloaders = new ArrayList();
private boolean bOverrideStartModeToStopped = false;
private boolean bDefaultForSeeding;
/** Things to be disposed of when window closes */
private ArrayList disposeList = new ArrayList();
private boolean bClosed = false;
/** Shell to use to open children (FileDialog, etc) */
private Shell shellForChildren;
private String sDestDir;
/**
*
* @param parent
* @param gm
* @param sPathOfFilesToOpen
* @param sFilesToOpen
* @param bDefaultStopped
* @param bForSeeding
*/
public synchronized static final void invoke(Shell parent, GlobalManager gm,
String sPathOfFilesToOpen, String[] sFilesToOpen,
boolean bDefaultStopped, boolean bForSeeding, boolean bPopupOpenURL) {
String saveSilentlyDir = null;
if (stOpenTorrentWindow == null) {
boolean bMustOpen = (sPathOfFilesToOpen == null && sFilesToOpen == null)
|| bForSeeding;
if (!bMustOpen) {
saveSilentlyDir = getSaveSilentlyDir();
bMustOpen = saveSilentlyDir == null;
}
stOpenTorrentWindow = new OpenTorrentWindow(parent, gm, bMustOpen);
} else {
if (stOpenTorrentWindow.shell != null)
stOpenTorrentWindow.shell.forceActive();
}
if (stOpenTorrentWindow != null) {
// local var because static may get set o null
OpenTorrentWindow openTorrentWindow = stOpenTorrentWindow;
openTorrentWindow.bOverrideStartModeToStopped = bDefaultStopped;
openTorrentWindow.bDefaultForSeeding = bForSeeding;
if (sFilesToOpen != null) {
// If none of the files sent to us were valid files, don't open the
// window
if (!bPopupOpenURL
&& openTorrentWindow.addTorrents(sPathOfFilesToOpen, sFilesToOpen) == 0
&& openTorrentWindow.torrentList.size() == 0
&& openTorrentWindow.downloaders.size() == 0) {
openTorrentWindow.close(true, true);
return;
}
}
if (bPopupOpenURL)
openTorrentWindow.browseURL();
if (saveSilentlyDir != null) {
openTorrentWindow.sDestDir = saveSilentlyDir;
openTorrentWindow.openTorrents();
openTorrentWindow.close(true, false);
}
}
}
/**
*
* @param parent
* @param gm
*/
public synchronized static final void invoke(final Shell parent,
GlobalManager gm) {
invoke(parent, gm, null, null, false, false, false);
}
public synchronized static final void invokeURLPopup(final Shell parent,
GlobalManager gm) {
invoke(parent, gm, null, null, false, false, true);
}
/**
*
* @param parent
* @param gm
* @param bOpenWindow
*/
private OpenTorrentWindow(final Shell parent, GlobalManager gm,
boolean bOpenWindow) {
this.gm = gm;
sDestDir = COConfigurationManager.getStringParameter(PARAM_DEFSAVEPATH, "");
if (bOpenWindow)
openWindow(parent);
else
shellForChildren = parent;
}
private void openWindow(Shell parent) {
boolean bTorrentInClipboard = false;
GridData gridData;
Label label;
Composite cArea;
shell = org.gudy.azureus2.ui.swt.components.shell.ShellFactory.createShell(
parent, SWT.RESIZE | SWT.DIALOG_TRIM);
shellForChildren = shell;
shell.setText(MessageText.getString("OpenTorrentWindow.title"));
Utils.setShellIcon(shell);
GridLayout layout = new GridLayout();
shell.setLayout(layout);
shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
resizeTables(3);
}
});
Clipboard clipboard = new Clipboard(shell.getDisplay());
String sClipText = (String) clipboard.getContents(TextTransfer
.getInstance());
if (sClipText != null)
bTorrentInClipboard = addTorrentsFromTextList(sClipText, true) > 0;
// label = new Label(shell, SWT.BORDER | SWT.WRAP);
// Messages.setLanguageText(label, "OpenTorrentWindow.message");
// gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
// label.setLayoutData(gridData);
// Torrents
// ========
Composite cButtons = new Composite(shell, SWT.NONE);
gridData = new GridData(GridData.FILL_HORIZONTAL);
cButtons.setLayoutData(gridData);
RowLayout rLayout = new RowLayout(SWT.HORIZONTAL);
rLayout.marginBottom = 0;
rLayout.marginLeft = 0;
rLayout.marginRight = 0;
rLayout.marginTop = 0;
cButtons.setLayout(rLayout);
// Buttons for tableTorrents
Button browseTorrent = new Button(cButtons, SWT.PUSH);
Messages.setLanguageText(browseTorrent, "OpenTorrentWindow.addFiles");
browseTorrent.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
FileDialog fDialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
fDialog.setFilterExtensions(new String[] { "*.torrent", "*.tor",
Constants.FILE_WILDCARD });
fDialog.setFilterNames(new String[] { "*.torrent", "*.tor",
Constants.FILE_WILDCARD });
fDialog.setFilterPath(TorrentOpener.getFilterPathTorrent());
fDialog.setText(MessageText.getString("MainWindow.dialog.choose.file"));
String fileName = TorrentOpener.setFilterPathTorrent(fDialog.open());
if (fileName != null) {
addTorrents(fDialog.getFilterPath(), fDialog.getFileNames());
}
}
});
Button browseURL = new Button(cButtons, SWT.PUSH);
Messages.setLanguageText(browseURL, "OpenTorrentWindow.addFiles.URL");
browseURL.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
browseURL();
}
});
Button browseFolder = new Button(cButtons, SWT.PUSH);
Messages.setLanguageText(browseFolder, "OpenTorrentWindow.addFiles.Folder");
browseFolder.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
DirectoryDialog fDialog = new DirectoryDialog(shell, SWT.NULL);
fDialog.setFilterPath(TorrentOpener.getFilterPathTorrent());
fDialog.setMessage(MessageText
.getString("MainWindow.dialog.choose.folder"));
String path = TorrentOpener.setFilterPathTorrent(fDialog.open());
if (path != null)
addTorrents(path, null);
}
});
if (bTorrentInClipboard) {
Button pasteOpen = new Button(cButtons, SWT.PUSH);
Messages.setLanguageText(pasteOpen,
"OpenTorrentWindow.addFiles.Clipboard");
pasteOpen.setToolTipText(sClipText);
pasteOpen.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
Clipboard clipboard = new Clipboard(shell.getDisplay());
String sClipText = (String) clipboard.getContents(TextTransfer
.getInstance());
if (sClipText != null) {
addTorrentsFromTextList(sClipText, false);
}
}
});
}
Group gTorrentsArea = new Group(shell, SWT.NONE);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gTorrentsArea.setLayoutData(gridData);
layout = new GridLayout();
gTorrentsArea.setLayout(layout);
Messages
.setLanguageText(gTorrentsArea, "OpenTorrentWindow.torrentLocation");
Composite cTorrentList = new Composite(gTorrentsArea, SWT.NONE);
gridData = new GridData(GridData.FILL_HORIZONTAL);
cTorrentList.setLayoutData(gridData);
createTorrentListArea(cTorrentList);
Composite cTorrentOptions = new Composite(gTorrentsArea, SWT.NONE);
gridData = new GridData(GridData.FILL_HORIZONTAL);
cTorrentOptions.setLayoutData(gridData);
layout = new GridLayout();
layout.marginHeight = 0;
cTorrentOptions.setLayout(layout);
CLabel clabel = new CLabel(cTorrentOptions, SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL);
clabel.setLayoutData(gridData);
Messages.setLanguageText(clabel, "OpenTorrentWindow.torrent.options");
int userMode = COConfigurationManager.getIntParameter("User Mode");
if (userMode > 0) {
Composite cTorrentModes = new Composite(cTorrentOptions, SWT.NONE);
gridData = new GridData(GridData.FILL_HORIZONTAL);
cTorrentModes.setLayoutData(gridData);
layout = new GridLayout();
layout.numColumns = 4;
layout.marginWidth = 0;
layout.marginHeight = 0;
cTorrentModes.setLayout(layout);
label = new Label(cTorrentModes, SWT.NONE);
gridData = new GridData(GridData.VERTICAL_ALIGN_CENTER);
label.setLayoutData(gridData);
Messages.setLanguageText(label, "OpenTorrentWindow.startMode");
cmbStartMode = new Combo(cTorrentModes, SWT.BORDER | SWT.READ_ONLY);
gridData = new GridData(GridData.FILL_HORIZONTAL);
cmbStartMode.setLayoutData(gridData);
updateStartModeCombo();
cmbStartMode.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setSelectedStartMode(cmbStartMode.getSelectionIndex());
}
});
label = new Label(cTorrentModes, SWT.NONE);
gridData = new GridData(GridData.VERTICAL_ALIGN_CENTER);
label.setLayoutData(gridData);
Messages.setLanguageText(label, "OpenTorrentWindow.addPosition");
cmbQueueLocation = new Combo(cTorrentModes, SWT.BORDER | SWT.READ_ONLY);
gridData = new GridData(GridData.FILL_HORIZONTAL);
cmbQueueLocation.setLayoutData(gridData);
updateQueueLocationCombo();
cmbQueueLocation.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setSelectedQueueLocation(cmbQueueLocation.getSelectionIndex());
}
});
}
// Save To..
// =========
Composite cSaveTo = new Composite(cTorrentOptions, SWT.NONE);
gridData = new GridData(GridData.FILL_HORIZONTAL);
cSaveTo.setLayoutData(gridData);
layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
layout.numColumns = 2;
cSaveTo.setLayout(layout);
label = new Label(cSaveTo, SWT.NONE);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
label.setLayoutData(gridData);
Messages.setLanguageText(label, "OpenTorrentWindow.dataLocation");
cmbDataDir = new Combo(cSaveTo, SWT.BORDER);
gridData = new GridData(GridData.FILL_HORIZONTAL);
cmbDataDir.setLayoutData(gridData);
cmbDataDir.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
sDestDir = cmbDataDir.getText();
int[] indexes = tableTorrents.getSelectionIndices();
for (int i = 0; i < indexes.length; i++) {
TorrentInfo info = (TorrentInfo) torrentList.get(indexes[i]);
if (!info.allFilesMoving())
info.sDestDir = sDestDir;
}
tableTorrents.clearAll();
updateOKButton();
}
});
cmbDataDir.setText(sDestDir);
final StringList dirList = COConfigurationManager
.getStringListParameter("saveTo_list");
StringIterator iter = dirList.iterator();
while (iter.hasNext()) {
cmbDataDir.add(iter.next());
}
Button browseData = new Button(cSaveTo, SWT.PUSH);
Messages.setLanguageText(browseData, "ConfigView.button.browse");
browseData.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
String sSavePath;
String sDefPath = cmbDataDir.getText();
if (sDefPath.length() > 0) {
File f = new File(sDefPath);
if (!f.exists()) {
f.mkdirs();
}
}
DirectoryDialog dDialog = new DirectoryDialog(shell, SWT.SYSTEM_MODAL);
dDialog.setFilterPath(sDefPath);
dDialog.setMessage(MessageText
.getString("MainWindow.dialog.choose.savepath_forallfiles"));
sSavePath = dDialog.open();
if (sSavePath != null) {
cmbDataDir.setText(sSavePath);
}
}
});
// File List
// =========
Group gFilesArea = new Group(shell, SWT.NONE);
gridData = new GridData(GridData.FILL_BOTH);
gFilesArea.setLayoutData(gridData);
layout = new GridLayout();
gFilesArea.setLayout(layout);
Messages.setLanguageText(gFilesArea, "OpenTorrentWindow.fileList");
createTableDataFiles(gFilesArea);
// Ok, cancel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -