use of com.biglybt.core.global.GlobalManagerAdapter in project BiglyBT by BiglySoftware.
the class TorrentUIUtilsV3 method _loadTorrent.
private static void _loadTorrent(final Core core, final DownloadUrlInfo dlInfo, // open player
final boolean playNow, // as for open player but don't actually open it
final boolean playPrepare, final boolean bringToFront) {
if (dlInfo instanceof DownloadUrlInfoSWT) {
DownloadUrlInfoSWT dlInfoSWT = (DownloadUrlInfoSWT) dlInfo;
dlInfoSWT.invoke(playNow ? "play" : "download");
return;
}
String url = dlInfo.getDownloadURL();
try {
Matcher m = hashPattern.matcher(url);
if (m.find()) {
String hash = m.group(1);
GlobalManager gm = core.getGlobalManager();
final DownloadManager dm = gm.getDownloadManager(new HashWrapper(Base32.decode(hash)));
if (dm != null) {
if (playNow || playPrepare) {
new AEThread2("playExisting", true) {
@Override
public void run() {
if (playNow) {
Debug.outNoStack("loadTorrent already exists.. playing", false);
TorrentListViewsUtils.playOrStream(dm, -1);
} else {
Debug.outNoStack("loadTorrent already exists.. preparing", false);
PlayUtils.prepareForPlay(dm);
}
}
}.start();
} else {
new MessageBoxShell(SWT.OK, MSG_ALREADY_EXISTS, new String[] { " ", dm.getDisplayName(), MessageText.getString(MSG_ALREADY_EXISTS_NAME) }).open(null);
}
return;
}
}
UIFunctionsSWT uiFunctions = (UIFunctionsSWT) UIFunctionsManager.getUIFunctions();
if (uiFunctions != null) {
// if (!COConfigurationManager.getBooleanParameter("add_torrents_silently")) { not used 11/30/2015
if (bringToFront) {
uiFunctions.bringToFront();
}
// }
Shell shell = uiFunctions.getMainShell();
if (shell != null) {
new FileDownloadWindow(shell, url, dlInfo.getReferer(), dlInfo.getRequestProperties(), null, new TorrentDownloaderCallBackInterface() {
@Override
public void TorrentDownloaderEvent(int state, TorrentDownloader inf) {
if (state == TorrentDownloader.STATE_FINISHED) {
File file = inf.getFile();
file.deleteOnExit();
// Do a quick check to see if it's a torrent
if (!TorrentUtil.isFileTorrent(dlInfo.getDownloadURL(), file, file.getName(), true)) {
return;
}
TOTorrent torrent;
try {
torrent = TorrentUtils.readFromFile(file, false);
} catch (TOTorrentException e) {
Debug.out(e);
return;
}
// Security: Only allow torrents from whitelisted trackers
if (playNow && !PlatformTorrentUtils.isPlatformTracker(torrent)) {
Debug.out("stopped loading torrent because it's not in whitelist");
return;
}
HashWrapper hw;
try {
hw = torrent.getHashWrapper();
} catch (TOTorrentException e1) {
Debug.out(e1);
return;
}
GlobalManager gm = core.getGlobalManager();
if (playNow || playPrepare) {
DownloadManager existingDM = gm.getDownloadManager(hw);
if (existingDM != null) {
if (playNow) {
TorrentListViewsUtils.playOrStream(existingDM, -1);
} else {
PlayUtils.prepareForPlay(existingDM);
}
return;
}
}
final HashWrapper fhw = hw;
GlobalManagerListener l = new GlobalManagerAdapter() {
@Override
public void downloadManagerAdded(DownloadManager dm) {
try {
core.getGlobalManager().removeListener(this);
handleDMAdded(dm, playNow, playPrepare, fhw);
} catch (Exception e) {
Debug.out(e);
}
}
};
gm.addListener(l, false);
TorrentOpener.openTorrent(file.getAbsolutePath());
}
}
});
}
}
} catch (Exception e) {
Debug.out(e);
}
}
use of com.biglybt.core.global.GlobalManagerAdapter in project BiglyBT by BiglySoftware.
the class SB_Transfers method setupViewTitleWithCore.
protected void setupViewTitleWithCore(Core _core) {
synchronized (SB_Transfers.class) {
if (!first) {
return;
}
first = false;
core = _core;
coreCreateTime = core.getCreateTime();
}
categoryListener = new CategoryListener() {
@Override
public void downloadManagerRemoved(Category cat, DownloadManager removed) {
RefreshCategorySideBar(cat);
}
@Override
public void downloadManagerAdded(Category cat, DownloadManager manager) {
RefreshCategorySideBar(cat);
}
};
paramCatInSidebarListener = new ParameterListener() {
@Override
public void parameterChanged(String parameterName) {
if (Utils.isAZ2UI()) {
return;
}
Category[] categories = CategoryManager.getCategories();
if (categories.length == 0) {
return;
}
boolean catInSidebar = COConfigurationManager.getBooleanParameter("Library.CatInSideBar");
if (catInSidebar) {
if (categoryManagerListener != null) {
return;
}
categoryManagerListener = new CategoryManagerListener() {
@Override
public void categoryRemoved(Category category) {
removeCategory(category);
}
@Override
public void categoryChanged(Category category) {
RefreshCategorySideBar(category);
}
@Override
public void categoryAdded(Category category) {
Category[] categories = CategoryManager.getCategories();
if (categories.length == 3) {
for (Category cat : categories) {
setupCategory(cat);
}
} else {
setupCategory(category);
}
}
};
CategoryManager.addCategoryManagerListener(categoryManagerListener);
if (categories.length > 2) {
for (Category category : categories) {
category.addCategoryListener(categoryListener);
setupCategory(category);
}
}
} else {
if (categoryManagerListener != null) {
CategoryManager.removeCategoryManagerListener(categoryManagerListener);
categoryManagerListener = null;
}
for (Category category : categories) {
category.removeCategoryListener(categoryListener);
removeCategory(category);
}
}
}
};
COConfigurationManager.addAndFireParameterListener("Library.CatInSideBar", paramCatInSidebarListener);
tagListener = new TagListener() {
@Override
public void taggableAdded(Tag tag, Taggable tagged) {
refreshTagSideBar(tag);
}
@Override
public void taggableSync(Tag tag) {
refreshTagSideBar(tag);
}
@Override
public void taggableRemoved(Tag tag, Taggable tagged) {
refreshTagSideBar(tag);
}
};
paramTagsInSidebarListener = new ParameterListener() {
@Override
public void parameterChanged(String parameterName) {
boolean tagInSidebar = COConfigurationManager.getBooleanParameter("Library.TagInSideBar");
if (tagInSidebar) {
addTagManagerListeners();
} else {
removeTagManagerListeners(true);
}
}
};
COConfigurationManager.addAndFireParameterListener("Library.TagInSideBar", paramTagsInSidebarListener);
final GlobalManager gm = core.getGlobalManager();
dmListener = new DownloadManagerAdapter() {
@Override
public void stateChanged(DownloadManager dm, int state) {
stateChanged(dm, state, statsNoLowNoise);
stateChanged(dm, state, statsWithLowNoise);
}
public void stateChanged(DownloadManager dm, int state, stats stats) {
if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
return;
}
synchronized (statsLock) {
updateDMCounts(dm);
boolean complete = dm.getAssumedComplete();
Boolean wasErrorStateB = (Boolean) dm.getUserData("wasErrorState");
boolean wasErrorState = wasErrorStateB != null && wasErrorStateB.booleanValue();
boolean isErrorState = state == DownloadManager.STATE_ERROR;
if (isErrorState != wasErrorState) {
int rel = isErrorState ? 1 : -1;
if (complete) {
stats.numErrorComplete += rel;
} else {
stats.numErrorInComplete += rel;
}
updateErrorTooltip(gm, stats);
dm.setUserData("wasErrorState", Boolean.valueOf(isErrorState));
}
refreshAllLibraries();
}
}
@Override
public void completionChanged(DownloadManager dm, boolean completed) {
completionChanged(dm, completed, statsNoLowNoise);
completionChanged(dm, completed, statsWithLowNoise);
}
public void completionChanged(DownloadManager dm, boolean completed, stats stats) {
if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
return;
}
synchronized (statsLock) {
int dm_state = updateDMCounts(dm);
if (completed) {
stats.numComplete++;
stats.numIncomplete--;
if (dm_state == DownloadManager.STATE_ERROR) {
stats.numErrorComplete++;
stats.numErrorInComplete--;
}
if (dm_state == DownloadManager.STATE_STOPPED) {
statsNoLowNoise.numStoppedIncomplete--;
}
} else {
stats.numComplete--;
stats.numIncomplete++;
if (dm_state == DownloadManager.STATE_ERROR) {
stats.numErrorComplete--;
stats.numErrorInComplete++;
}
if (dm_state == DownloadManager.STATE_STOPPED) {
statsNoLowNoise.numStoppedIncomplete++;
}
}
recountUnopened();
updateErrorTooltip(gm, stats);
refreshAllLibraries();
}
}
};
gmListener = new GlobalManagerAdapter() {
@Override
public void downloadManagerRemoved(DownloadManager dm) {
downloadManagerRemoved(dm, statsNoLowNoise);
downloadManagerRemoved(dm, statsWithLowNoise);
}
public void downloadManagerRemoved(DownloadManager dm, stats stats) {
if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
return;
}
synchronized (statsLock) {
recountUnopened();
if (dm.getAssumedComplete()) {
stats.numComplete--;
Boolean wasDownloadingB = (Boolean) dm.getUserData("wasDownloading");
if (wasDownloadingB != null && wasDownloadingB.booleanValue()) {
stats.numDownloading--;
}
} else {
stats.numIncomplete--;
Boolean wasSeedingB = (Boolean) dm.getUserData("wasSeeding");
if (wasSeedingB != null && wasSeedingB.booleanValue()) {
stats.numSeeding--;
}
}
Boolean wasStoppedB = (Boolean) dm.getUserData("wasStopped");
boolean wasStopped = wasStoppedB != null && wasStoppedB.booleanValue();
if (wasStopped) {
if (!dm.getAssumedComplete()) {
stats.numStoppedIncomplete--;
}
}
Boolean wasQueuedB = (Boolean) dm.getUserData("wasQueued");
boolean wasQueued = wasQueuedB != null && wasQueuedB.booleanValue();
if (wasQueued) {
stats.numQueued--;
}
refreshAllLibraries();
}
dm.removeListener(dmListener);
}
@Override
public void downloadManagerAdded(DownloadManager dm) {
dm.addListener(dmListener, false);
synchronized (statsLock) {
recountUnopened();
downloadManagerAdded(dm, statsNoLowNoise);
downloadManagerAdded(dm, statsWithLowNoise);
refreshAllLibraries();
}
}
public void downloadManagerAdded(DownloadManager dm, stats stats) {
if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
return;
}
boolean assumed_complete = dm.getAssumedComplete();
synchronized (statsLock) {
if (dm.isPersistent() && dm.getTorrent() != null && !assumed_complete) {
// ignore borked torrents as their create time is inaccurate
stats.newestIncompleteDownloadTime = Math.max(stats.newestIncompleteDownloadTime, dm.getCreationTime());
}
int dm_state = dm.getState();
if (assumed_complete) {
stats.numComplete++;
if (dm_state == DownloadManager.STATE_SEEDING) {
stats.numSeeding++;
}
} else {
stats.numIncomplete++;
if (dm_state == DownloadManager.STATE_DOWNLOADING) {
dm.setUserData("wasDownloading", Boolean.TRUE);
stats.numDownloading++;
} else {
dm.setUserData("wasDownloading", Boolean.FALSE);
}
}
}
}
};
gm.addListener(gmListener, false);
resetStats(gm, dmListener, statsWithLowNoise, statsNoLowNoise);
refreshAllLibraries();
timerEventPeriodic = SimpleTimer.addPeriodicEvent("header:refresh", 60 * 1000, new TimerEventPerformer() {
@Override
public void perform(TimerEvent event) {
stats withNoise = new stats();
stats noNoise = new stats();
noNoise.includeLowNoise = false;
withNoise.includeLowNoise = true;
synchronized (statsLock) {
resetStats(gm, null, withNoise, noNoise);
boolean fixed = false;
if (!withNoise.sameAs(statsWithLowNoise)) {
statsWithLowNoise.copyFrom(withNoise);
fixed = true;
}
if (!noNoise.sameAs(statsNoLowNoise)) {
statsNoLowNoise.copyFrom(noNoise);
fixed = true;
}
if (fixed) {
updateErrorTooltip(gm, statsWithLowNoise);
updateErrorTooltip(gm, statsNoLowNoise);
refreshAllLibraries();
}
}
}
});
}
Aggregations