use of com.biglybt.core.CoreRunningListener in project BiglyBT by BiglySoftware.
the class TorrentOpener method openTorrentsForTracking.
protected static void openTorrentsForTracking(final String path, final String[] fileNames) {
CoreWaiterSWT.waitForCoreRunning(new CoreRunningListener() {
@Override
public void coreRunning(final Core core) {
final Display display = Utils.getDisplay();
if (display == null || display.isDisposed() || core == null)
return;
new AEThread2("TorrentOpener") {
@Override
public void run() {
for (int i = 0; i < fileNames.length; i++) {
try {
TOTorrent t = TorrentUtils.readFromFile(new File(path, fileNames[i]), true);
core.getTrackerHost().hostTorrent(t, true, true);
} catch (Throwable e) {
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Torrent open fails for '" + path + File.separator + fileNames[i] + "'", e));
}
}
}
}.start();
}
});
}
use of com.biglybt.core.CoreRunningListener in project BiglyBT by BiglySoftware.
the class TorrentOpener method addTorrent.
/**
* @param torrentOptions
* @return
* @since 5.0.0.1
*
* @TODO: Remove SWT UI parts (use UIFunctions) and move out of SWT tree
*/
public static final boolean addTorrent(final TorrentOpenOptions torrentOptions) {
try {
if (torrentOptions.getTorrent() == null) {
return false;
}
final DownloadManagerInitialisationAdapter dmia = new DownloadManagerInitialisationAdapter() {
@Override
public int getActions() {
return (ACT_ASSIGNS_TAGS);
}
@Override
public void initialised(DownloadManager dm, boolean for_seeding) {
DiskManagerFileInfoSet file_info_set = dm.getDiskManagerFileInfoSet();
DiskManagerFileInfo[] fileInfos = file_info_set.getFiles();
boolean reorder_mode = COConfigurationManager.getBooleanParameter("Enable reorder storage mode");
int reorder_mode_min_mb = COConfigurationManager.getIntParameter("Reorder storage mode min MB");
try {
dm.getDownloadState().suppressStateSave(true);
boolean[] toSkip = new boolean[fileInfos.length];
boolean[] toCompact = new boolean[fileInfos.length];
boolean[] toReorderCompact = new boolean[fileInfos.length];
int[] priorities = null;
int comp_num = 0;
int reorder_comp_num = 0;
final TorrentOpenFileOptions[] files = torrentOptions.getFiles();
for (int iIndex = 0; iIndex < fileInfos.length; iIndex++) {
DiskManagerFileInfo fileInfo = fileInfos[iIndex];
if (iIndex >= 0 && iIndex < files.length && files[iIndex].lSize == fileInfo.getLength()) {
// Always pull destination file from fileInfo and not from
// TorrentFileInfo because the destination may have changed
// by magic code elsewhere
File fDest = fileInfo.getFile(true);
if (files[iIndex].isLinked()) {
fDest = files[iIndex].getDestFileFullName();
// Can't use fileInfo.setLink(fDest) as it renames
// the existing file if there is one
dm.getDownloadState().setFileLink(iIndex, fileInfo.getFile(false), fDest);
}
if (files[iIndex].isToDownload()) {
int priority = files[iIndex].getPriority();
if (priority != 0) {
if (priorities == null) {
priorities = new int[fileInfos.length];
}
priorities[iIndex] = priority;
}
} else {
toSkip[iIndex] = true;
if (!fDest.exists()) {
if (reorder_mode && (fileInfo.getLength() / (1024 * 1024)) >= reorder_mode_min_mb) {
toReorderCompact[iIndex] = true;
reorder_comp_num++;
} else {
toCompact[iIndex] = true;
comp_num++;
}
}
}
}
}
if (files.length == 1) {
TorrentOpenFileOptions file = files[0];
if (file.isManualRename()) {
String fileRename = file.getDestFileName();
if (fileRename != null && fileRename.length() > 0) {
dm.getDownloadState().setDisplayName(fileRename);
}
}
} else {
String folderRename = torrentOptions.getManualRename();
if (folderRename != null && folderRename.length() > 0) {
dm.getDownloadState().setDisplayName(folderRename);
}
}
if (comp_num > 0) {
file_info_set.setStorageTypes(toCompact, DiskManagerFileInfo.ST_COMPACT);
}
if (reorder_comp_num > 0) {
file_info_set.setStorageTypes(toReorderCompact, DiskManagerFileInfo.ST_REORDER_COMPACT);
}
file_info_set.setSkipped(toSkip, true);
if (priorities != null) {
file_info_set.setPriority(priorities);
}
int maxUp = torrentOptions.getMaxUploadSpeed();
int kInB = DisplayFormatters.getKinB();
if (maxUp > 0) {
dm.getStats().setUploadRateLimitBytesPerSecond(maxUp * kInB);
}
int maxDown = torrentOptions.getMaxDownloadSpeed();
if (maxDown > 0) {
dm.getStats().setDownloadRateLimitBytesPerSecond(maxDown * kInB);
}
DownloadManagerState dm_state = dm.getDownloadState();
if (torrentOptions.disableIPFilter) {
dm_state.setFlag(DownloadManagerState.FLAG_DISABLE_IP_FILTER, true);
}
if (torrentOptions.peerSource != null) {
for (String peerSource : torrentOptions.peerSource.keySet()) {
boolean enable = torrentOptions.peerSource.get(peerSource);
dm_state.setPeerSourceEnabled(peerSource, enable);
}
}
Map<String, Boolean> enabledNetworks = torrentOptions.getEnabledNetworks();
if (enabledNetworks != null) {
if (!dm_state.getFlag(DownloadManagerState.FLAG_INITIAL_NETWORKS_SET)) {
for (String net : enabledNetworks.keySet()) {
boolean enable = enabledNetworks.get(net);
dm_state.setNetworkEnabled(net, enable);
}
}
}
List<Tag> initialTags = torrentOptions.getInitialTags();
for (Tag t : initialTags) {
t.addTaggable(dm);
}
List<List<String>> trackers = torrentOptions.getTrackers(true);
if (trackers != null) {
TOTorrent torrent = dm.getTorrent();
TorrentUtils.listToAnnounceGroups(trackers, torrent);
try {
TorrentUtils.writeToFile(torrent);
} catch (Throwable e2) {
Debug.printStackTrace(e2);
}
}
if (torrentOptions.bSequentialDownload) {
dm_state.setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, true);
}
File moc = torrentOptions.getMoveOnComplete();
if (moc != null) {
dm_state.setAttribute(DownloadManagerState.AT_MOVE_ON_COMPLETE_DIR, moc.getAbsolutePath());
}
} finally {
dm.getDownloadState().suppressStateSave(false);
}
}
};
CoreFactory.addCoreRunningListener(new CoreRunningListener() {
@Override
public void coreRunning(Core core) {
TOTorrent torrent = torrentOptions.getTorrent();
byte[] hash = null;
try {
hash = torrent.getHash();
} catch (TOTorrentException e1) {
}
int iStartState = (torrentOptions.getStartMode() == TorrentOpenOptions.STARTMODE_STOPPED) ? DownloadManager.STATE_STOPPED : DownloadManager.STATE_QUEUED;
GlobalManager gm = core.getGlobalManager();
DownloadManager dm = gm.addDownloadManager(torrentOptions.sFileName, hash, torrentOptions.getParentDir(), torrentOptions.getSubDir(), iStartState, true, torrentOptions.getStartMode() == TorrentOpenOptions.STARTMODE_SEEDING, dmia);
// since gm.addDown.. will handle it.
if (dm == null) {
return;
}
if (torrentOptions.iQueueLocation == TorrentOpenOptions.QUEUELOCATION_TOP) {
gm.moveTop(new DownloadManager[] { dm });
}
if (torrentOptions.getStartMode() == TorrentOpenOptions.STARTMODE_FORCESTARTED) {
dm.setForceStart(true);
}
}
});
} catch (Exception e) {
UIFunctions uif = UIFunctionsManager.getUIFunctions();
if (uif != null) {
uif.showErrorMessage("OpenTorrentWindow.mb.openError", Debug.getStackTrace(e), new String[] { torrentOptions.sOriginatingLocation, e.getMessage() });
}
return false;
}
return true;
}
use of com.biglybt.core.CoreRunningListener in project BiglyBT by BiglySoftware.
the class ProgressPanel method makeTorrent.
public void makeTorrent() {
int tracker_type = wizard.getTrackerType();
if (tracker_type == NewTorrentWizard.TT_EXTERNAL) {
TrackersUtil.getInstance().addTracker(wizard.trackerURL);
}
File f;
if (wizard.create_mode == NewTorrentWizard.MODE_DIRECTORY) {
f = new File(wizard.directoryPath);
} else if (wizard.create_mode == NewTorrentWizard.MODE_SINGLE_FILE) {
f = new File(wizard.singlePath);
} else {
f = wizard.byo_desc_file;
}
try {
URL url = new URL(wizard.trackerURL);
final TOTorrent torrent;
if (wizard.getPieceSizeComputed()) {
wizard.creator = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength(f, url, wizard.getAddOtherHashes());
wizard.creator.addListener(this);
wizard.creator.setFileIsLayoutDescriptor(wizard.create_mode == NewTorrentWizard.MODE_BYO);
torrent = wizard.creator.create();
} else {
wizard.creator = TOTorrentFactory.createFromFileOrDirWithFixedPieceLength(f, url, wizard.getAddOtherHashes(), wizard.getPieceSizeManual());
wizard.creator.addListener(this);
wizard.creator.setFileIsLayoutDescriptor(wizard.create_mode == NewTorrentWizard.MODE_BYO);
torrent = wizard.creator.create();
}
if (tracker_type == NewTorrentWizard.TT_DECENTRAL) {
TorrentUtils.setDecentralised(torrent);
}
torrent.setComment(wizard.getComment());
TorrentUtils.setDHTBackupEnabled(torrent, wizard.permitDHT);
TorrentUtils.setPrivate(torrent, wizard.getPrivateTorrent());
LocaleTorrentUtil.setDefaultTorrentEncoding(torrent);
// mark this newly created torrent as complete to avoid rechecking on open
final File save_dir;
if (wizard.create_mode == NewTorrentWizard.MODE_DIRECTORY) {
save_dir = f;
} else if (wizard.create_mode == NewTorrentWizard.MODE_SINGLE_FILE) {
save_dir = f.getParentFile();
} else {
String save_path = COConfigurationManager.getStringParameter("Default save path");
File f_save_path = new File(save_path);
if (!f_save_path.canWrite()) {
throw (new Exception("Default save path is not configured: See Tools->Options->File"));
}
save_dir = f_save_path;
}
if (wizard.useMultiTracker) {
this.reportCurrentTask(MessageText.getString("wizard.addingmt"));
TorrentUtils.listToAnnounceGroups(wizard.trackers, torrent);
}
if (wizard.useWebSeed && wizard.webseeds.size() > 0) {
this.reportCurrentTask(MessageText.getString("wizard.webseed.adding"));
Map ws = wizard.webseeds;
List getright = (List) ws.get("getright");
if (getright.size() > 0) {
for (int i = 0; i < getright.size(); i++) {
reportCurrentTask(" GetRight: " + getright.get(i));
}
torrent.setAdditionalListProperty("url-list", new ArrayList(getright));
}
List webseed = (List) ws.get("webseed");
if (webseed.size() > 0) {
for (int i = 0; i < webseed.size(); i++) {
reportCurrentTask(" WebSeed: " + webseed.get(i));
}
torrent.setAdditionalListProperty("httpseeds", new ArrayList(webseed));
}
}
// must do this last as it saves a copy of the torrent state for future opening...
/*
* actually, don't need to do this as the "open-for-seeding" option used when adding the download
* does the job. Reason I stopped doing this is
* https://sourceforge.net/tracker/index.php?func=detail&aid=1721917&group_id=84122&atid=575154
*
DownloadManagerState download_manager_state =
DownloadManagerStateFactory.getDownloadState( torrent );
TorrentUtils.setResumeDataCompletelyValid( download_manager_state );
download_manager_state.save();
*/
this.reportCurrentTask(MessageText.getString("wizard.maketorrent.savingfile"));
final File torrent_file = new File(wizard.savePath);
torrent.serialiseToBEncodedFile(torrent_file);
this.reportCurrentTask(MessageText.getString("wizard.maketorrent.filesaved"));
wizard.switchToClose(new Runnable() {
@Override
public void run() {
show_torrent_file.setEnabled(true);
}
});
if (wizard.autoOpen) {
CoreWaiterSWT.waitForCore(TriggerInThread.NEW_THREAD, new CoreRunningListener() {
@Override
public void coreRunning(Core core) {
boolean start_stopped = COConfigurationManager.getBooleanParameter("Default Start Torrents Stopped");
byte[] hash = null;
try {
hash = torrent.getHash();
} catch (TOTorrentException e1) {
}
if (wizard.forceStart || wizard.superseed) {
// superseeding can only be set for an active download...
start_stopped = false;
}
DownloadManagerInitialisationAdapter dmia;
final String initialTags = wizard.getInitialTags(true);
if (initialTags.length() > 0) {
dmia = new DownloadManagerInitialisationAdapter() {
@Override
public int getActions() {
return (ACT_ASSIGNS_TAGS);
}
@Override
public void initialised(DownloadManager dm, boolean for_seeding) {
TagManager tm = TagManagerFactory.getTagManager();
TagType tag_type = tm.getTagType(TagType.TT_DOWNLOAD_MANUAL);
String[] bits = initialTags.replace(';', ',').split(",");
for (String tag : bits) {
tag = tag.trim();
if (tag.length() > 0) {
try {
Tag t = tag_type.getTag(tag, true);
if (t == null) {
t = tag_type.createTag(tag, true);
}
t.addTaggable(dm);
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
};
} else {
dmia = null;
}
final DownloadManager dm = core.getGlobalManager().addDownloadManager(torrent_file.toString(), hash, save_dir.toString(), start_stopped ? DownloadManager.STATE_STOPPED : // persistent
DownloadManager.STATE_QUEUED, // persistent
true, // for seeding
true, dmia);
if (!start_stopped && dm != null) {
// We want this to move to seeding ASAP, so move it to the top
// of the download list, where it will do the quick check and
// move to the seeding list
// (the for seeding flag should really be smarter and verify
// it's a seeding torrent and set appropriately)
dm.getGlobalManager().moveTop(new DownloadManager[] { dm });
}
if (wizard.autoHost && wizard.getTrackerType() != NewTorrentWizard.TT_EXTERNAL) {
try {
core.getTrackerHost().hostTorrent(torrent, true, false);
} catch (TRHostException e) {
Logger.log(new LogAlert(LogAlert.REPEATABLE, "Host operation fails", e));
}
}
if (dm != null) {
if (wizard.forceStart) {
dm.setForceStart(true);
}
if (wizard.superseed) {
new AEThread2("startwait") {
@Override
public void run() {
long start = SystemTime.getMonotonousTime();
while (true) {
if (dm.isDestroyed()) {
break;
}
long elapsed = SystemTime.getMonotonousTime() - start;
if (elapsed > 60 * 1000) {
int state = dm.getState();
if (state == DownloadManager.STATE_ERROR || state == DownloadManager.STATE_STOPPED) {
break;
}
}
if (elapsed > 5 * 60 * 1000) {
break;
}
PEPeerManager pm = dm.getPeerManager();
if (pm != null) {
pm.setSuperSeedMode(true);
break;
}
try {
Thread.sleep(1000);
} catch (Throwable e) {
break;
}
}
}
}.start();
}
}
}
});
}
} catch (Exception e) {
if (e instanceof TOTorrentException) {
TOTorrentException te = (TOTorrentException) e;
if (te.getReason() == TOTorrentException.RT_CANCELLED) {
// expected failure, don't log exception
} else {
reportCurrentTask(MessageText.getString("wizard.operationfailed"));
reportCurrentTask(TorrentUtils.exceptionToText(te));
}
} else {
Debug.printStackTrace(e);
reportCurrentTask(MessageText.getString("wizard.operationfailed"));
reportCurrentTask(Debug.getStackTrace(e));
}
wizard.switchToClose();
}
}
use of com.biglybt.core.CoreRunningListener in project BiglyBT by BiglySoftware.
the class SBC_SearchResultsView method skinObjectInitialShow.
public Object skinObjectInitialShow(SWTSkinObject skinObject, Object params) {
image_loader = new ImageLoader(null, null);
CoreFactory.addCoreRunningListener(new CoreRunningListener() {
@Override
public void coreRunning(Core core) {
initColumns(core);
}
});
SWTSkinObjectTextbox soFilterBox = (SWTSkinObjectTextbox) getSkinObject("filterbox");
if (soFilterBox != null) {
txtFilter = soFilterBox.getTextControl();
}
if (vitality_images == null) {
ImageLoader loader = ImageLoader.getInstance();
vitality_images = loader.getImages("image.sidebar.vitality.dots");
ok_image = loader.getImage("tick_mark");
fail_image = loader.getImage("progress_cancel");
auth_image = loader.getImage("image.sidebar.vitality.auth");
}
final SWTSkinObject soFilterArea = getSkinObject("filterarea");
if (soFilterArea != null) {
SWTSkinObjectToggle soFilterButton = (SWTSkinObjectToggle) getSkinObject("filter-button");
if (soFilterButton != null) {
boolean toggled = COConfigurationManager.getBooleanParameter("Search View Filter Options Expanded", false);
if (toggled) {
soFilterButton.setToggled(true);
soFilterArea.setVisible(true);
}
soFilterButton.addSelectionListener(new SWTSkinToggleListener() {
@Override
public void toggleChanged(SWTSkinObjectToggle so, boolean toggled) {
COConfigurationManager.setParameter("Search View Filter Options Expanded", toggled);
soFilterArea.setVisible(toggled);
Utils.relayout(soFilterArea.getControl().getParent());
}
});
}
Composite parent = (Composite) soFilterArea.getControl();
Composite filter_area = new Composite(parent, SWT.NONE);
FormData fd = Utils.getFilledFormData();
filter_area.setLayoutData(fd);
GridLayout layout = new GridLayout();
layout.marginBottom = layout.marginTop = layout.marginLeft = layout.marginRight = 0;
filter_area.setLayout(layout);
Label label;
int sepHeight = 20;
Composite cRow = new Composite(filter_area, SWT.NONE);
cRow.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
rowLayout.spacing = 5;
rowLayout.marginBottom = rowLayout.marginTop = rowLayout.marginLeft = rowLayout.marginRight = 0;
rowLayout.center = true;
cRow.setLayout(rowLayout);
// with/without keywords
ImageLoader imageLoader = ImageLoader.getInstance();
for (int i = 0; i < 2; i++) {
final boolean with = i == 0;
if (!with) {
label = new Label(cRow, SWT.VERTICAL | SWT.SEPARATOR);
label.setLayoutData(new RowData(-1, sepHeight));
}
Composite cWithKW = new Composite(cRow, SWT.NONE);
layout = new GridLayout(2, false);
layout.marginWidth = 0;
layout.marginBottom = layout.marginTop = layout.marginLeft = layout.marginRight = 0;
cWithKW.setLayout(layout);
// Label lblWithKW = new Label(cWithKW, SWT.NONE);
// lblWithKW.setText(MessageText.getString(with?"SubscriptionResults.filter.with.words":"SubscriptionResults.filter.without.words"));
Label lblWithKWImg = new Label(cWithKW, SWT.NONE);
lblWithKWImg.setImage(imageLoader.getImage(with ? "icon_filter_plus" : "icon_filter_minus"));
final Text textWidget = new Text(cWithKW, SWT.BORDER);
if (with) {
textWithKW = textWidget;
} else {
textWithoutKW = textWidget;
}
textWidget.setMessage(MessageText.getString(with ? "SubscriptionResults.filter.with.words" : "SubscriptionResults.filter.without.words"));
GridData gd = new GridData();
gd.widthHint = Utils.adjustPXForDPI(100);
textWidget.setLayoutData(gd);
textWidget.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
String text = textWidget.getText().toLowerCase(Locale.US);
String[] bits = text.split("\\s+");
Set<String> temp = new HashSet<>();
for (String bit : bits) {
bit = bit.trim();
if (bit.length() > 0) {
temp.add(bit);
}
}
String[] words = temp.toArray(new String[temp.size()]);
synchronized (filter_lock) {
if (with) {
with_keywords = words;
} else {
without_keywords = words;
}
}
refilter_dispatcher.dispatch();
}
});
}
// min size
label = new Label(cRow, SWT.VERTICAL | SWT.SEPARATOR);
label.setLayoutData(new RowData(-1, sepHeight));
Composite cMinSize = new Composite(cRow, SWT.NONE);
layout = new GridLayout(2, false);
layout.marginWidth = 0;
layout.marginBottom = layout.marginTop = layout.marginLeft = layout.marginRight = 0;
cMinSize.setLayout(layout);
Label lblMinSize = new Label(cMinSize, SWT.NONE);
lblMinSize.setText(MessageText.getString("SubscriptionResults.filter.min_size"));
spinMinSize = new Spinner(cMinSize, SWT.BORDER);
spinMinSize.setMinimum(0);
// 100 TB should do...
spinMinSize.setMaximum(100 * 1024 * 1024);
spinMinSize.setSelection(minSize);
spinMinSize.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
minSize = ((Spinner) event.widget).getSelection();
refilter();
}
});
// max size
label = new Label(cRow, SWT.VERTICAL | SWT.SEPARATOR);
label.setLayoutData(new RowData(-1, sepHeight));
Composite cMaxSize = new Composite(cRow, SWT.NONE);
layout = new GridLayout(2, false);
layout.marginWidth = 0;
layout.marginBottom = layout.marginTop = layout.marginLeft = layout.marginRight = 0;
cMaxSize.setLayout(layout);
Label lblMaxSize = new Label(cMaxSize, SWT.NONE);
lblMaxSize.setText(MessageText.getString("SubscriptionResults.filter.max_size"));
spinMaxSize = new Spinner(cMaxSize, SWT.BORDER);
spinMaxSize.setMinimum(0);
// 100 TB should do...
spinMaxSize.setMaximum(100 * 1024 * 1024);
spinMaxSize.setSelection(maxSize);
spinMaxSize.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
maxSize = ((Spinner) event.widget).getSelection();
refilter();
}
});
engine_area = new Composite(filter_area, SWT.NONE);
engine_area.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
buildEngineArea(null);
parent.layout(true);
}
return null;
}
use of com.biglybt.core.CoreRunningListener in project BiglyBT by BiglySoftware.
the class ConfigView method initialize.
private void initialize(final Composite composite) {
// need to initalize composite now, since getComposite can
// be called at any time
cConfig = new Composite(composite, SWT.NONE);
GridLayout configLayout = new GridLayout();
configLayout.marginHeight = 0;
configLayout.marginWidth = 0;
cConfig.setLayout(configLayout);
GridData gridData = new GridData(GridData.FILL_BOTH);
Utils.setLayoutData(cConfig, gridData);
final Label label = new Label(cConfig, SWT.CENTER);
Messages.setLanguageText(label, "view.waiting.core");
gridData = new GridData(GridData.FILL_BOTH);
Utils.setLayoutData(label, gridData);
// Need to delay initialation until core is done so we can guarantee
// all config sections are loaded (ie. plugin ones).
// TODO: Maybe add them on the fly?
CoreFactory.addCoreRunningListener(new CoreRunningListener() {
@Override
public void coreRunning(Core core) {
Utils.execSWTThread(new AERunnable() {
@Override
public void runSupport() {
_initialize(composite);
label.dispose();
composite.layout(true, true);
}
});
}
});
}
Aggregations